RecyclerView单击可将数据传递到新活动

时间:2016-11-11 05:21:07

标签: android onclick android-recyclerview

我正在创建一个带有三个标签的应用程序,每个标签都有一个带有两个文本的recyclerview,我希望用户能够点击任何recyclerview项目,并且该项目中的文本应该传递给新活动。< / p>

This is an image of how the app looks like.

这是我的MainActivity代码

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
    viewPager.setAdapter(pagerAdapter);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    // Iterate over all tabs and set the custom view
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setCustomView(pagerAdapter.getTabView(i));
    }

}


@Override
public void onResume() {
    super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Tab 1", "Tab 2", "Tab 3" };
    Context context;

    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new OneFragment();
            case 1:
                return new TwoFragment();
            case 2:
                return new ThreeFragment();
        }

        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

    public View getTabView(int position) {
        View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) tab.findViewById(R.id.custom_text);
        tv.setText(tabTitles[position]);
        return tab;
    }

}

}

这是我的一个片段的代码

public class OneFragment extends Fragment {

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_one, container, false);

    RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
    rv.setHasFixedSize(true);
    MyAdapter adapter = new MyAdapter(new String[]{"1 Grace", "test two", "test three", "test four", "test five", "test six", "test seven"});
    rv.setAdapter(adapter);

    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    rv.setLayoutManager(llm);

    return rootView;
}

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
    private static final int LENGTH = 50;

    public ContentAdapter() {
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.singleitemview, parent, false));
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), SingleItemView.class);
            Toast.makeText(getActivity(),
                    "Your Message", Toast.LENGTH_LONG).show();
            startActivity(intent);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
    }

    @Override
    public int getItemCount() {
        return LENGTH;
    }
}

}

我还有一个ViewHolder类

public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
    super(inflater.inflate(R.layout.singleitemview, parent, false));
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            Intent intent = new Intent(context, SingleItemView.class);
            context.startActivity(intent);
        }
    });
}

}

现在我想打开SingleItemView.class来显示回收站视图中的项目

请帮帮我

2 个答案:

答案 0 :(得分:2)

传入intent extras.there有几种方法可用,比如putString,putLong,putXXX等。如果它是你的自定义类然后实现serialize接口然后把data.and放在相应的activity中,使用getIntent.getExtras(&#) 34;传递将数据放入附加内容时使用的相同密钥&#34;)。并且您可以获得所需的数据。

答案 1 :(得分:2)

您可以将数据集传递到适配器构造函数,并在单击项目时引用数据集。大部分技巧都在ViewHolder实现中。假设数据集包含String个值,它应该或多或少看起来像这样:

public class ViewHolder extends RecyclerView.ViewHolder {
  public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
    super(inflater.inflate(R.layout.singleitemview, parent, false));
    itemView.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        int position = getAdapterPosition();

        Context context = v.getContext();
        Intent intent = new Intent(context, SingleItemView.class);
        intent.putExtra("com.package.sample.ITEM_DATA", mDataset.get(position));
        context.startActivity(intent);
      }
    });
  }
}