gpt4 book ai didi

java - 如何从 CustomAdapter 调用 Activity 中的方法

转载 作者:行者123 更新时间:2023-11-29 05:01:51 24 4
gpt4 key购买 nike

我在 feed_item.xml 中有一个 image,它在 custom adapter 中膨胀。我能够处理图像的点击事件.

我的 activity_main 中有一个相对布局,当我的 Main 时呈现 player.setVisibility(View.GONE); Activity 已启动。当我在行中的图像被单击时,我想 setVisibility(View.VISIBLE) 此布局。

我在将这两个布局与我的Custom AdapterMain Activity 相关联时遇到了问题。我们将不胜感激!

FeedListAdapter.java

public class FeedListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<FeedItem> feedItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
this.activity = activity;
this.feedItems = feedItems;
}

@Override
public int getCount() {
return feedItems.size();
}

@Override
public Object getItem(int location) {
return feedItems.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.feed_item, null);


TextView name = (TextView) convertView.findViewById(R.id.name);

FeedItem item = feedItems.get(position);

name.setText(item.getName());
String fontPath = "fonts/Lato-Light.ttf";

// Loading Font Face
Typeface tf = Typeface.createFromAsset(activity.getAssets(), fontPath);

// Applying font
name.setTypeface(tf);

ImageView play=(ImageView)convertView.findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, "Show Layout!", Toast.LENGTH_SHORT).show();
}
});
ImageView options=(ImageView)convertView.findViewById(R.id.options_popup);
options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showFilterPopup(view);
}
});

return convertView;
}

}

MainActivity.java

public class MainActivity extends ActionBarActivity {

RelativeLayout player;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
TextView mToolBarTextView = (TextView) findViewById(R.id.text_view_toolbar_title);
mToolBarTextView.setText("Home");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

listView = (InnerGridView) findViewById(R.id.list);

feedItems = new ArrayList<FeedItem>();

listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);

player = (RelativeLayout) findViewById(R.id.player);
player.setVisibility(View.GONE);
}
}

最佳答案

你可以用这样的方法创建你自己的界面

 public interface OnClickInMyAdapterListener {
public void onItemclicked();
}

你的 Activity 应该实现这个接口(interface)并覆盖它的方法

public class MainActivity extends ActionBarActivity implements OnClickInMyAdapterListener  {
...
...
}


@Override
public void onItemclicked() {
// do what you want
}

更改您的适配器以接收接口(interface)

     private OnClickInMyAdapterListener  myActivityInterface;

public FeedListAdapter(Activity activity, List<FeedItem> feedItems, OnClickInMyAdapterListener myActivityInterface ) {
this.activity = activity;
this.feedItems = feedItems;
this.myActivityInterface= myActivityInterface;

}

在 Activity 中将接口(interface)传递给适配器

listAdapter = new FeedListAdapter(this, feedItems, (OnClickInMyAdapterListener ) this);

并在你的适配器中任何你想要的地方使用它

this.myActivityInterface.onItemclicked();

...如果您的适配器用于更多不同的 Activity ,此解决方案也很有用

关于java - 如何从 CustomAdapter 调用 Activity 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31879598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com