0

这可能是一个重复的问题,但我查看了很多 SO 问题,但没有找到任何帮助。

我有一个使用 ActionBar 的应用程序,它有 2 个选项卡。每个都是 ListFragment,我无法选择其中的任何项目。他们会很好地填充,但 onListItemClick 不会触发。如果我将 setOnItemClickListener 直接用于我的 ListView,它也不会触发。但是,如果我只是扩展 Fragment 而不是 ListFragment,如果我将 onClickListener 附加到每一行,则 onClick 将触发。但这将允许多行可点击,这不是我想要发生的事情。谁能帮我弄清楚我做错了什么?提前致谢。

这是我的主要活动:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the action bar
        Drawable drawable = getResources().getDrawable(R.drawable.action_bar);
        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(drawable);
        bar.addTab(bar.newTab().setText("Tab 1").setTabListener(new TabHandler()));
        bar.addTab(bar.newTab().setText("Tab 2").setTabListener(new TabHandler()));
        bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowHomeEnabled(true);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setDisplayShowTitleEnabled(false);
    }
}

这是我的 TabHandler:

public class TabHandler implements ActionBar.TabListener {
    public Fragment mFragment;

    public TabHandler() { }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        switch (tab.getPosition()) {
            case 0:
                if(mFragment == null) {
                    mFragment = new ListFragmentA();
                    fragmentTransaction.add(android.R.id.content, mFragment, null);
                }
                else {
                    fragmentTransaction.show(mFragment); //Looking into detach and attach as well
                }
                break;
            case 1:
                if(mFragment == null) {
                    mFragment = new ListFragmentB();
                    fragmentTransaction.add(android.R.id.content, mFragment, null);
                }
                else {
                    fragmentTransaction.show(mFragment);
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        if (mFragment != null) {
            mFragment.getFragmentManager().popBackStackImmediate();
            fragmentTransaction.hide(mFragment);
        }
   }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        mFragment.getFragmentManager().popBackStackImmediate();
    }
}

这是我的片段之一的示例:

public class ListFragmentA extends ListFragment {
MyCustomAdapter myCustomAdapter;
ListView myListView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.my_list_background,container,false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(getActivity() != null) {
        myListView = (ListView)getActivity().findViewById(android.R.id.list);
        if(myListView != null) {
            CrudService itemService = new CrudService<TestItem>(TestItem.class);
            ArrayList<TestItem> myItems = (ArrayList<TestItem>) itemService.FindAll();
            myCustomAdapter = new MyCustomAdapter(getActivity(), myItems);
            myListView.setAdapter(myCustomAdapter);
            myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            myListView.setOnItemClickListener(new OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Log.i("ListFragmentA", "MyListView onItemClick");
                }                
            });
        }
    }
}
//This isn't getting fired
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Log.i("ListFragmentA", "onListItemClick at position " + position);
}

public class MyCustomAdapter extends ArrayAdapter<TestItem> {
    Context mContext;
    ArrayList<TestItem> mItems;

    public MyCustomAdapter(Context context, ArrayList<TestItem> items) {
        super(context, android.R.id.list, items);
        mContext = context;
        mItems = items;
    }

    public int getCount() {
        return mItems.size();
    }

    public TestItem getItem(int position) {
        return mItems.get(position);
    }

    public long getItemId(int position) {
        return mItems.get(position).getId();
    }

    public View getView(final int position, View convertView, ViewGroup arg2) {
        View rowView = convertView;
        if(rowView == null) {
            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.single_item, null);

            TestItemViewHolder viewHolder = new TestItemViewHolder();

            viewHolder.Name = (TextView)rowView.findViewById(R.id.txt_TestItem_Name);
            //I have a few more things
            rowView.setTag(viewHolder);
        }

        TestItemListViewHolder holder = (TestItemListViewHolder)rowView.getTag();

        TestItem gottenItem = getItem(position);
        holder.Name.setText(gottenItem.getName());
        //I have a few more things
        //However, this will fire if not a ListFragment
        rowView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i("ListFragmentA", "RowView onClick at position " + position);
            }
        });
        return rowView;
    }
}
}

这是我的“my_list_background”xml 的示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Tab_Layout_A"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="@color/white">

<!-- listview Title Layout -->
<LinearLayout 
    android:id="@+id/ll_Table_Title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_marginBottom="-15dp"
    android:layout_marginTop="7dp"
    android:layout_marginLeft="11dip"
    android:layout_marginRight="11dip"
    android:orientation="horizontal"
    android:weightSum="100"
    android:background="@color/black">

    <TextView
        android:id="@+id/txt_TestItem_Name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Name"
        android:textColor="@color/white"
        android:textStyle="bold" />
<!--I have omitted the other columns-->
</LinearLayout>

<LinearLayout
    android:id="@+id/message_List_Layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">
    <ListView 
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>    
</LinearLayout>
</LinearLayout>

和我的“single_item”xml的一个例子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/single_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" 
android:gravity="center_vertical"
android:weightSum="100">

<TextView 
    android:id="@+id/txt_TestItem_Name"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:padding="2dip"
    android:layout_weight="25"
    android:textColor="@color/black"/>
<ImageButton 
    android:id="@+id/btn_Next"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:padding="2dip"
    android:layout_weight="2"
    android:background="@drawable/disclosure_button"
    android:contentDescription="@string/next_Button"/>
</LinearLayout>
4

1 回答 1

0

好的,所以我发现了我的问题。在我的“single_item”xml 文件中,我有一个 ImageButton 正在窃取焦点。我把它换成了 ImageView,它工作得很好。

于 2013-02-14T22:08:12.237 回答