【问题标题】:How to Implement onItemClick() for multiple ListViews separately?如何分别为多个 ​​ListView 实现 onItemClick()?
【发布时间】:2016-01-08 19:27:12
【问题描述】:

我正在为一本书开发一个应用程序,它有 13 个单元,每个单元包含许多练习,每个练习包含许多扫描图像。

现在我有一个 Main Menu Activity 和 2 个片段,fragmentA 用于在 ListView 中显示练习,fragmentB 用于显示练习的扫描图像。

我的主菜单活动包含 13 个按钮,每个按钮对应每个单元,用于加载该单元的练习。

我已经为每个单元分别创建了练习的 ArrayLists,以便在用户点击特定单元时将它们加载到 fragmentA 中。

我在 Main Menu Activity 中为每个单元的按钮单击创建了一个 Switch Case,它在列表视图中的 fragmentA 中加载该特定单元的 ArrayList。

Onbackpress 将用户从 fragmentA 带回到主菜单。

现在我正在尝试实现 OnItemClick() 以实现对 ListViews 的点击,这些点击会为每个单元的每个练习加载到 fragmentA 中。

如果我在 fragmentA OncreatView 中设置 toast,它会将所有单元的所有 ListView 分配给它们。 但是我想为每个单元的每个练习分别做,即我所有的 ArrayLists 中的所有条目。

正如我使用bundle将数据从ArayLists传递到fragmentA,像这样

 @Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button1:
            bundle = new Bundle();
            bundle.putStringArrayList(LISTVIEW_DATA, unit1);
            break;
        case R.id.button2:
            bundle = new Bundle();
            bundle.putStringArrayList(LISTVIEW_DATA, unit2);
            break;
        case R.id.button3:
            bundle = new Bundle();
            bundle.putStringArrayList(LISTVIEW_DATA,  unit3);
            break;
 default:
            break;
    }

    setListFragment(bundle);
}
private void setListFragment(Bundle arguments) {
    Fragment fragment = new ExFragment();

    if (arguments != null) {
        fragment.setArguments(arguments);
    }

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.replace(R.id.container, fragment);
    ft.addToBackStack("");
    ft.commit();
}

这是我在 fragmentA 中的 onItemClick。

@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)

我知道它非常基本的问题,但我花了好几个小时。

谢谢

我有一个用于 13 个 ArrayList 的片段 A,一次只显示一个,我正在寻找一种方法将 onitemclick 分配给 13 个列表视图中的所有项目。

【问题讨论】:

  • 我有点迷茫,你能不能给我们一些详细的信息?
  • 我已经更新了问题,如果你不明白,请询问
  • @DhawalSodhaParmar 我的问题有很大不同,我正在尝试处理多个列表视图的 onitemclick,在我的情况下,加载片段时一次只有一个数组可见
  • 这太令人困惑了,我很好奇,我可以看到您确实需要帮助(并且通过您的更新,您显然正在为此努力)。所以我会为此开始赏金,请确保接受解决您问题的答案,否则它会浪费掉。此外,人们可能需要更多详细信息,因此请尽可能提供。

标签: java android android-fragments


【解决方案1】:

对于遇到与我相同情况的任何人,我为每个案例分配了一个名为 DATA_TYPE 的标签,并为片段 on create view 中的每个标签创建了 switch case 并解决了问题

  @Override
public void onClick(View view) {
switch (view.getId()){
    case R.id.button1:
        bundle = new Bundle();
        bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE1);
        bundle.putStringArrayList(LISTVIEW_DATA, items1);
        break;
    case R.id.button2:
        bundle = new Bundle();
        bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE2);
        bundle.putStringArrayList(LISTVIEW_DATA, items2);
        break;
    case R.id.button3:
        bundle = new Bundle();
        bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE3);
        bundle.putStringArrayList(LISTVIEW_DATA, items3);
        break;
    default:
        break;
}

setListFragment(bundle);
}

MainActivityFragment 是片段类的名称。 希望对某人有所帮助

【讨论】:

    【解决方案2】:
    public class MyViewHolder extends RecyclerView.ViewHolder implements 
        View.OnClickListener {
    
        TextView name,  summary,  publishT;
        NetworkImageView  imageView;
        Button sharesNews;
    
        List<NewsDataModel> highlights = new ArrayList<NewsDataModel>();
        Context ctx;
    
        @SuppressWarnings("SpellCheckingInspection")
        public MyViewHolder(View itemView, Context ctx, List<NewsDataModel> 
          highlights) {
    
            super( itemView );
            this.ctx = ctx;
            this.highlights = highlights;
    
            name = itemView.findViewById( R.id.titlehgh );
            summary = itemView.findViewById( R.id.summaryhgh );
            publishT = itemView.findViewById( R.id.publishtH );
            imageView = itemView.findViewById( R.id.thumbnailhgh );
    
            itemView.setOnClickListener( this );
    
            sharesNews = itemView.findViewById( R.id.shares );
    
        }
    
        @Override
        public void onClick(View view) {
    
            int position = getAdapterPosition();
            NewsDataModel newsDataModel = this.highlights.get( position );
            Intent intent = new Intent ( this.ctx, NewsDetailsActivity.class );
    
            intent.putExtra( "id", newsDataModel.getId() );
            this.ctx.startActivity( intent );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多