简体   繁体   English

如何分别为多个ListView实现onItemClick()?

[英]How to Implement onItemClick() for multiple ListViews separately?

I am developing an app for a book, which have 13 units, each unit contain many exercises and each exercise contains many scanned images. 我正在为一本书开发一个应用程序,该应用程序有13个单元,每个单元包含许多练习,每个练习包含许多扫描图像。

Now I have a Main Menu Activity, and 2 fragments, fragmentA for showing the exercises in ListView and fragmentB for showing the scanned images of exercises. 现在,我有一个主菜单活动和2个片段,fragmentA用于在ListView中显示练习,fragmentB用于显示练习的扫描图像。

My Main Menu Activity contain 13 buttons, each button for every unit, to load that unit's exercises. 我的主菜单活动包含13个按钮,每个单元的每个按钮用于加载该单元的练习。

I have created ArrayLists of exercises for each unit separately, to load them into fragmentA when user tap on a specific unit. 我已经为每个单元分别创建了练习的ArrayList,以便在用户点击特定单元时将它们加载到fragmentA中。

I have created a Switch Case in the Main Menu Activity for button clicks of each unit, which loads the ArrayList of that specific unit in fragmentA in a listview. 我已经在Main Menu Activity(主菜单活动)中为每个单元的按钮单击创建了一个Switch Case,它将特定单元的ArrayList加载到listview的fragmentA中。

Onbackpress it takes user back from fragmentA to Main Menu. Onbackpress会将用户从fragmentA带回到主菜单。

Now I am trying to implement OnItemClick() for clicks on ListViews which are loaded into fragmentA for each exercise of each unit. 现在,我尝试实现OnItemClick(),以实现对ListViews的点击,对于每个单元的每次练习,ListViews都加载到fragmentA中。

If I set toast inside fragmentA OncreatView it assign them all of the ListViews of all the units. 如果我在fragmentA OncreatView中设置烤面包,则将它们分配给所有单元的所有ListView。 But I want to do separately for each exercise of each unit ie for all the entries in my all ArrayLists. 但是我想对每个单元的每个练习分别做,即对我所有ArrayLists中的所有条目做。

As am using bundle to pass the data from ArayLists into fragmentA, like this 使用束将数据从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();
}

This is my onItemClick in fragmentA. 这是我在fragmentA中的onItemClick。

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

I know its pretty basic question but am on it for hours. 我知道它的基本问题,但要花几个小时。

Thanks 谢谢

I have one fragmentA for 13 ArrayLists, showing only one at a time, and am looking for a way to assign onitemclick to all the items in 13 listviews. 我有一个fragmentA用于13个ArrayList,一次只显示一个,并且正在寻找一种方法,将onitemclick分配给13个列表视图中的所有项目。

For anyone who is going through the same situation as I was, I assigned a tag named DATA_TYPE to each case and made switch case for each tag in on create view of fragment and solved the issue 对于遇到与我一样的情况的人,我给每个案例分配了一个名为DATA_TYPE标签, on create view在片段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);
}

while MainActivityFragment is the name of fragment class. MainActivityFragment是片段类的名称。 Hope it helps to someone 希望对别人有帮助

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 );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM