CardView片段中的多个活动

时间:2015-08-17 14:05:01

标签: java android android-fragments

在浏览活动列表时实现switch语句的正确方法是什么?

例如,在我的片段中,有3张卡片视图,其中每张卡片视图导致不同的活动。

不幸的是,我还是新手,甚至我搜索了多个网站,但没有人无法提供我想要的结果。这就是我目前取得的进展。

希望你们中的一些人可以在这方面进一步指导我或提供可能有用的链接。

package info.androidhive.materialdesign.activity;


import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Selection;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import info.androidhive.materialdesign.R;

import static android.app.PendingIntent.getActivity;

/**
 * Created by venkataprasad.kukka on 02-01-2015.
 */
public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {

    private static ArrayList<FeddProperties> dataSet;

    public CardViewDataAdapter(ArrayList<FeddProperties> os_versions) {

        dataSet = os_versions;
    }


    @Override
    public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// create a new view
        View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                R.layout.card_view, null);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CardViewDataAdapter.ViewHolder viewHolder, int i) {

        FeddProperties fp = dataSet.get(i);

        viewHolder.tvVersionName.setText(fp.getTitle());
        viewHolder.iconView.setImageResource(fp.getThumbnail());
        viewHolder.feed = fp;
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    // inner class to hold a reference to each item of RecyclerView
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvVersionName;
        public ImageView iconView;

        public FeddProperties feed;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            tvVersionName = (TextView) itemLayoutView
                    .findViewById(R.id.tvVersionName);
            iconView = (ImageView) itemLayoutView
                    .findViewById(R.id.iconId);

            itemLayoutView.setOnClickListener(new AdapterView.OnClickListener() {

                @Override
                public void onClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                    Intent intent = null;
                    switch (position) {
                        case 0:
                            intent = new Intent(this, FirstActivity.class);
                            break;

                        case 1:
                            intent = new Intent(this, SecondActivity.class);
                            break;

                        Toast.makeText(getActivity().getApplicationContext(), "You Clicked at " + feed.getTitle(), Toast.LENGTH_SHORT).show();
                        ((Selection) getActivity()).onSelection(position);
                    }

                }

            });
        }
    }
}

0 个答案:

没有答案