片段中的ListView Clickable和on item clicked listener

时间:2013-04-02 21:40:29

标签: android listview onclick android-fragments onclicklistener

我在片段中有一个列表视图,这是我当前的代码:

public static class AllSectionFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public AllSectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tasks_all,
                container, false);
        // /////////////////////////////////////////////////////////////////
        // Set up all components here:: ie text views , buttons lists etc.//
        // that correspond to the layout fragment xml file                //
        ////////////////////////////////////////////////////////////////////
        TextView dummyTextView = (TextView) rootView
                .findViewById(R.id.fragment_tasks_all_textView);
        dummyTextView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));

        ArrayList<String> arrayList = new ArrayList<String>();
        ListView allList = (ListView) rootView
                .findViewById(R.id.fragment_tasks_all_list);
        MyCustomAdapter mAdapter = new MyCustomAdapter(getActivity(),
                arrayList); // Class to populate a ListView with an
                            // ArrayList
        allList.setAdapter(mAdapter);
        // Populate array list
        for (int i = 0; i < 5; i++) {
            arrayList.add(" All Task " + i);
        }
        mAdapter.notifyDataSetChanged();

        return rootView;
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        System.out.println("pos: "+ position);
    }
}

和我的layout.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TasksActivity$DummySectionFragment" >

<TextView
    android:id="@+id/fragment_tasks_all_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/fragment_tasks_all_list"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="0.41"
    android:cacheColorHint="#00000000"
    android:clickable="true"
    android:listSelector="@android:color/transparent"
    android:transcriptMode="alwaysScroll" >

</ListView>

首先。我该怎么做才能点击列表中的项目

,其次是如何为它添加onclick监听器。

3 个答案:

答案 0 :(得分:2)

为什么不使用ListFragment:

  public static class ArrayListFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, Shakespeare.TITLES));
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}

答案 1 :(得分:0)

使用ArrayAdapter的替代构造函数,它允许您指定一个布局文件来托管列表视图:

public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

答案 2 :(得分:0)

我遇到了类似的问题,并通过使用onClick方法创建接口并在片段中实现它来解决。 尝试这样做:

创建一个界面:

<强> MyOnViewClickListener.java

public interface MyOnViewClickListener {
    public void myOnViewClickListener(View v);
}

在自定义适配器类中,创建一个构造函数,将MyOnViewClickListener作为参数传递:

private Context context;
private MyOnViewClickListener itemListener;
private List<String> items;

public MyCustomAdapter(Context context, List<String> items, MyOnViewClickListener itemListener) {

        this.itemListener = itemListener;
        this.context = context;
        this.items = items;
}

getView()方法中,放置:

LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.your_row_layout, parent, false);

TextView textView1 = (TextView) row.findViewById(R.id.quizchallenge);
    textView1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            itemListener.myOnViewClickListener(v);
        }
});

最后,在您的片段类中,实现新界面

public class ListFriendFragment extends Fragment implements MyOnViewClickListener  {
.
.
.
@Override
public void myOnViewClickListener(View v){
    //call your method
    quickQuiz();
}

设置适配器:

adapter = new MyCustomAdapter(getContext(), myItemsList, MyFragment.this);