方法listview setOnItemClickListener()在Fragment中不起作用

时间:2015-07-25 18:19:01

标签: android listview android-listview

Listview lisetOnItemClickListener()无法正常工作。下面是我的活动和listview xml。我做了android:focusable =“false”,android:focusableInTouchMode =“false”和android:clickable =“false”。它仍然没有用。请帮忙。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/white"
android:orientation="horizontal"
android:padding="6dp"
android:descendantFocusability="blocksDescendants">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@+id/business_image"
        android:layout_width="@dimen/business_icon_width"
        android:layout_height="@dimen/business_icon_height"
        android:scaleType="fitXY"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"/>

    <ProgressBar
        android:id="@+id/invoice_pgb"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"/>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/content_ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="5dp"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

    <LinearLayout
        android:id="@+id/business_name_11"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:descendantFocusability="blocksDescendants">

        <TextView
            android:id="@+id/business_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1.0"
            android:ellipsize="end"
            android:maxLines="2"
            android:textColor="@color/black"
            android:textSize="15sp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"
            />

        <TextView
            android:id="@+id/invoice_total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="14sp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"/>
    </LinearLayout>

    <!--ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hotel_desc"
        android:layout_marginTop="3dp"
        android:background="@drawable/star_5"
        android:scaleType="fitXY" /-->
    <!--TextView
        android:id="@+id/paynow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:background="@color/color1"
        android:padding="5dp"
        android:text="Pay Now"
        android:textColor="@color/white"
        android:textSize="12sp" /-->

    <Button
        android:text="Pay Now"
        android:id="@+id/btpaynow"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:textColor="@color/white"
        android:background="@drawable/login_button_login_page"
        android:textSize="10sp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        />
    <TextView
        android:id="@+id/invoice_type"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/business_name_11"
        android:layout_toLeftOf="@+id/btpaynow"
        android:ellipsize="end"
        android:maxLines="2"
        android:textColor="@color/gray"
        android:textSize="14sp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"/>

    <TextView
        android:id="@+id/business_location"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/invoice_type"
        android:ellipsize="end"
        android:maxLines="2"
        android:textColor="@color/gray"
        android:textSize="10sp"
        android:text="location"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"/>


</RelativeLayout>

@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);
    View v = getView();
    listview = (ListView) v.findViewById(R.id.listView);

    setdata();

}

private void setdata() {
Database db = new Database(this.getActivity());
    ArrayList<ListPageDataSet> listPageDataRecords = new ArrayList<ListPageDataSet>();
    listPageDataRecords=db.getListPageData();

    for (int i = 0; i < listPageDataRecords.size(); i++) {
        a.add(listPageDataRecords.get(i).a());
        b.add(listPageDataRecords.get(i).b());
        c.add(listPageDataRecords.get(i).c()+" for "+listPageDataRecords.get(i).d());

    }   
madapter = new InvoiceListingAdapter(this.getActivity(), a, b, c,d);

listview.setAdapter(animationAdapter);
    listview.setItemsCanFocus(false);
    listview.setTextFilterEnabled(true);

    ListView listview2 = (ListView) getView().findViewById(R.id.listView);

    listview2.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

            Toast t = Toast.makeText(getActivity(), "Message",
                    Toast.LENGTH_SHORT);
            t.show();
            Intent intent = new Intent(InvoiceMainActivity.this.getActivity(), DetailsPage.class);
            startActivity(intent);

        }
    });         

    }
}

1 个答案:

答案 0 :(得分:0)

public class MyListFragment extends ListFragment implements OnItemClickListener {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.list_fragment, container, false);
      return view;
   }

   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Planets, android.R.layout.simple_list_item_1);
      setListAdapter(adapter);
      getListView().setOnItemClickListener(this);
   }

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
      Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
   }
}