人造人.不工作 ListFragment onListItemClick()



我有一个使用 TabLayout 5 片段的活动,一个使用 mapview 的片段,另一个使用"ListFragment"的片段,但我有一个问题:

ListFragment 未激活 onListItemClick((

我做了很多,但很难解决。

这代码我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:descendantFocusability="blocksDescendants"
tools:context = "com.prebation.myapplication.h1_page">
<android.support.design.widget.TabLayout
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="@color/cast_libraries_material_featurehighlight_outer_highlight_default_color"
    app:tabSelectedTextColor="@color/cast_intro_overlay_button_background_color" />
<com.prebation.myapplication.NonSwipeViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:layout_height="match_parent"
    android:layout_below="@id/tab" />

^ 活动 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginRight="5dp"
android:background="@drawable/round_angle"
android:orientation="horizontal">
<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center"
    app:srcCompat="@drawable/hosp" />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/list_hos_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="병원이름"
            android:textSize="18sp" />
        <TextView
            android:id="@+id/list_hos_distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:text="병원거리" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <RatingBar
            android:id="@+id/list_hos_star"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:isIndicator="true"
            android:rating="2.5" />

        <TextView
            android:id="@+id/list_hos_review"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_weight="1"
            android:text="리뷰" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:background="@color/cast_expanded_controller_ad_label_background_color"
            android:text="대기인원"
            android:textColor="@android:color/white" />
        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_weight="1"
            android:background="@color/cast_expanded_controller_ad_label_background_color"
            android:text="접수/예약"
            android:textColor="@android:color/white" />
        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_weight="1"
            android:background="@color/cast_expanded_controller_ad_label_background_color"
            android:text="결제가능"
            android:textColor="@android:color/white" />
    </LinearLayout>
</LinearLayout>
<ImageButton
    android:id="@+id/list_call_bt"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:layout_weight="1"
    android:focusable="false"
    android:background="@android:color/white"
    app:srcCompat="@android:drawable/sym_action_call" />

^ list_layout_xml使用一个图像视图,一个评级栏,一个图像按钮,六个文本视图

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    adapter = new Listview_Fragment_item_Adapter() ;

    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6121","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6122","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6123","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6124","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6125","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6126","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6127","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6129","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6129","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6129","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6129","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    adapter.addItem("연세 내과","서울특별시 강남구 수서동 715","리뷰(50)","010-8424-6129","평일 09:30 ~ 18:30","주말 09:30 ~ 12:30");
    setListAdapter(adapter) ;
    return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onListItemClick (ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // get TextView's Text.
    Listview_Fragment_item item = (Listview_Fragment_item) l.getItemAtPosition(position) ;
   // Listview_Fragment_item items = (Listview_Fragment_item) adapter.getItem(position);
    String hos_name = item.getHos_name();
    String hos_adress= item.getHos_adress();
    String hos_review = item.getHos_review();
    String hos_phone = item.getHos_phone();
    String hos_day_time = item.getHos_day_time();
    String hos_week_time = item.getHos_week_time();
    Log.d("리스트 클릭", hos_phone);
    Intent intent = new Intent(getActivity(),Clk_Fragment_list.class);
    intent.putExtra("hos_name",hos_name);
    intent.putExtra("hos_adress",hos_adress);
    intent.putExtra("hos_review",hos_review);
    intent.putExtra("hos_phone",hos_phone);
    intent.putExtra("hos_day_time",hos_day_time);
    intent.putExtra("hos_week_time",hos_week_time);
    startActivity(intent);
    // TODO : use item data.
}
单击

图像按钮,但不单击列表项。

如果您的代码尝试单击片段中的项目,您可能需要像下面的代码一样执行此操作。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.list_container_id, container, false);

    // Create a list of items
    final ArrayList<Listview_Fragment_item> items= new ArrayList<Listview_Fragment_item>();     
    items.add(new Listview_Fragment_item("some String", "another String","other String"));
    items.add(new Listview_Fragment_item("some String", "another String","other String"));
    items.add(new Listview_Fragment_item("some String", "another String","other String"));
    items.add(new Listview_Fragment_item("some String", "another String","other String"));
    items.add(new Listview_Fragment_item("some String", "another String","other String"));

    // Create an {@link Listview_Fragment_item_Adapter}, whose data source is a list of {@link Listview_Fragment_item}s. The
    // adapter knows how to create list items for each item in the list.
    Listview_Fragment_item_Adapter adapter = new Listview_Fragment_item_Adapter(getActivity(), items);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID which is the id of your list container(in this case list_container_id,
    //which is declared in the xml which contains the list item        
    ListView listView = (ListView) rootView.findViewById(R.id.list_container_id);
    // Make the {@link ListView} use the {@link Listview_Fragment_item_Adapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Listview_Fragment_item} in the list.
    listView.setAdapter(adapter);

    // Set a click listener to perform the instruction when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            // Get the {@link Listview_Fragment_item } object at the given position the user clicked on
            Listview_Fragment_item item = items.get(position);
            //Now the current item has been get, you can now use it for the remaining codes.

        }
    });
    //This will then return the created view.
    return rootView;
}

最新更新