我有:public class FeedApp extends Fragment {
.....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
....
listAdapter = new FeedListAdapter(getActivity(), feedItems);
listView.setAdapter(listAdapter);
....
在我的自定义适配器FeedListAdapter
中,我有一个button "Show/hide"
。
如果我点击我可以看到描述(TextView),否则我隐藏描述(TextView)。
public static void toggle_contents(View v){
switch(v.getId()){
case R.id.showHide:
description.setVisibility( description.isShown() ? View.GONE : View.VISIBLE );
break;
}
我的适配器:
public class FeedListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Content> feedItems;
private TextView title;
private TextView description;
TextView showHide;
public FeedListAdapter(Activity activity, List<Content> 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);
title = (TextView) convertView.findViewById(R.id.title);
description = (TextView) convertView.findViewById(R.id.description);
showHide= (TextView) convertView.findViewById(R.id.hideShow);
Content item = feedItems.get(position);
title.setText(item.getTitle());
description.setText(item.getDescription());
return convertView;
}
我的大问题是我必须如何以及在何处实施onClick();
我尝试使用我的适配器,但它不起作用。 有人可以给我一些建议吗??? Thx
答案 0 :(得分:1)
尝试在适配器中使用此代码:
private SparseArray<Boolean> mVisibleDesc = new SparseArray<Boolean>();
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Content item = feedItems.get(position);
if (inflater == null) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
// Inflate item layout
convertView = inflater.inflate(R.layout.feed_item, null);
// Set the description
final TextView description = (TextView) convertView.findViewById(R.id.description);
description.setText(item.getDescription());
description.setTag(position);
// Set the listener
TextView showHide = (TextView) convertView.findViewById(R.id.hideShow);
showHide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Get the position of this item
Integer position = (Integer) description.getTag();
if (mVisibleDesc.get(position)) {
// true
description.setVisibility(View.GONE);
mVisibleDesc.put(position, false);
} else {
// false or null (not yet set)
description.setVisibility(View.VISIBLE);
mVisibleDesc.put(position, true);
}
}
});
} else {
// ConvertView not null, set the description
TextView description = (TextView) convertView.findViewById(R.id.description);
description.setText(item.getDescription());
description.setTag(position);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(item.getTitle());
return convertView;
}
关于它的几点说明:
description
知道它所在的项目,以便听众可以知道要隐藏/显示哪一个。description
,title
和showHide
。他们应该使用getView
方法保持在本地。如果您对此有任何问题,请回复。
答案 1 :(得分:0)
如果onClick()不适合您,您可以在按钮声明中对方法调用进行硬编码。只需转到布局xml文件中的按钮的XML声明,然后添加以下行:
android:onClick="toggle_contents"
这应该可以解决问题。如果它不妨碍其他任何地方,请从方法中删除“静态”。它适用于简单的Activity,它也应该在Fragment中。
答案 2 :(得分:0)
将android:onClick="toggleContents"
放入XML文件中的按钮声明中,然后创建
附加了与您的片段的活动中的public void toggleContents(View v)
方法