【问题标题】:Communication between Fragments and ListFragmentFragments 和 ListFragment 之间的通信
【发布时间】:2015-08-13 15:44:40
【问题描述】:

我在一个活动中有两个片段,我的第一个片段正在扩展 ListFragment 类,并且在其 xml 布局中有一个 ID 为“列表”的 ListView 小部件。我为此列表编写了一个自定义适配器并且工作正常,但我需要单击一个项目以将一些值传递给另一个片段。我猜对于 itemClick 事件,我必须使用 ListFragment 类的 onListItemClick() 方法。但是当我运行应用程序并单击列表中的项目时,什么也没发生。

public class List extends ListFragment {

    private Context context;
    ListAdapter listAdapter;
    OnHeadlineSelectedListener mCallback;

    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }


    @Override
    public void onCreate (Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        KnlContainer knlModel = new KnlContainer();
        listAdapter = new ListAdapter((Activity)context,knlModel);

    }

    @Override
    public void onAttach(Activity activity){

        super.onAttach(activity);
        context = activity;

      //I need to call this instance of interface to comminacation with Activity
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }

    }
    @Override
     public void onListItemClick(ListView l, View v, int position, long id) {

      // Send the event to the host activity

       //for comminacation with activity

        mCallback.onArticleSelected(position);
            Log.e("STAT","OK");
        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        LinearLayout rootView = (LinearLayout)inflater.inflate(R.layout.knl_list_fragment, null);

        ListView knllrList = (ListView)rootView.findViewById(android.R.id.list);
        knllrList.setAdapter(listAdapter);


        return rootView;

    }
}

我必须在适配器中定义 clicklistener 吗?

【问题讨论】:

    标签: android android-fragments android-activity fragment


    【解决方案1】:

    适配器纯粹用于处理数据并为列表视图中的每一行生成视图。

    根据 Android ListFragment API,您必须使用 ListFragment.setListAdapter()

    所以....

     @Override
     public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    
        KnlContainer knlModel = new KnlContainer();
    
        setListAdapter(new ListAdapter((Activity)context, knlModel));
     }
    

    【讨论】:

    • 我如你所说从适配器获取数据,但我想将 clicklistener 添加到列表视图中的每个项目,因为我需要通过单击这些不同的项目在另一个片段中显示结果。单击按钮时,它必须调用 public void onListItemClick 但它不起作用
    • 如果我理解你的话,你的 ListView 中的每个项目(行)都有一个按钮?
    • 是的,我的自定义行视图中有按钮,但我不知道如何使用 clicklistener。我必须在我的自定义适配器中添加 clicklistener 吗?或者我需要使用 ListFragment 的 onListItemClick() 方法?
    • 该按钮是不必要的,因为整个行视图都是可点击的。
    【解决方案2】:

    如果您的列表包含其他一些可点击元素,例如复选框,您必须将其focusable 属性设置为false,然后onListItemClick 才能工作。

    看到这个答案:ListFragment OnListItemClick not being called


    如果这不起作用,另一个可能的解决方案是确保 yourListView 的 id 定义为 android:id="android:id/list"不是 android:id="@+id/list

    【讨论】:

      猜你喜欢
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多