我设计了一个ListFragment
。在itemLongClick()
,活动开始。 onActivityResult()
,我再次设置适配器并动态创建新的文本视图。项目中每个视图的onClick
,会显示Toast
。如果我在适配器中的每个视图上实现onClick()
,则itemLongClick()
无效。
我在下面附上了我的代码:
在ListFragment
内:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
clickPosition = position;
Intent intent = new Intent(parent.getContext(),AddEventActivity.class );
startActivityForResult(intent, CREATE_EVENT);
return true;
}
});
}
适配器getView()
内的:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View)inflater.inflate(R.layout.event_listview_items, null);
TextView hour = (TextView)view.findViewById(R.id.hour);
LinearLayout layout = (LinearLayout)view.findViewById(R.id.layout);
hour.setText(hourAL.get(position));
if(eventAL.size() != 0){
for(int i=0;i<eventAL.size();i++){
if(eventAL.get(i).getPosition() == position){
for(int j =0;j<eventAL.get(i).eventName.size();j++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LayoutParams.FILL_PARENT,1.0f);
params.setMargins(3, 3, 3, 3);
final TextView txt = new TextView(parent.getContext());
txt.setId(++textid);
txt.setBackgroundColor(Color.GRAY);
txt.setLayoutParams(params);
txt.setText(eventAL.get(i).eventName.get(j));
layout.addView(txt);
txt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(parent.getContext(), txt.getId() + " clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
return view;
}
请有人指导我如何实现这两项功能。
答案 0 :(得分:1)
我找到了解决方案。 对于我的动态视图,我将setLongClickable()设为true。