如何使用卡片视图创建循环视图一旦点击卡片查看它是否打开了新的片段活动?

时间:2016-09-01 17:13:09

标签: android android-fragments android-recyclerview android-cardview

如何使用卡片视图创建循环视图一旦点击卡片查看它是否打开了新的片段活动?

我想创建android app for tutorials我成功创建了带卡片视图的列表视图

我喜欢在点击带有卡片视图的列表视图时如何打开新片段

该片段将在加载我的网站网址

的新活动中显示

Www.techinfoworld.com

如何做到这一点请帮帮我

这里我附上了卡片型号view link

的列表视图

MainActivity.java

package androidwarriors.recycleviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);

        RecyclerAdapter adapter=new RecyclerAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
}

RecyclerAdapter.java

package androidwarriors.recycleviewdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


public class RecyclerAdapter extends  RecyclerView.Adapter<RecyclerViewHolder> {

    String [] name={"Androidwarriors","Stackoverflow","Developer Android","AndroidHive",
            "Slidenerd","TheNewBoston","Truiton","HmkCode","JavaTpoint","Javapeper"};
Context context;
    LayoutInflater inflater;
    public RecyclerAdapter(Context context) {
        this.context=context;
        inflater=LayoutInflater.from(context);
    }
    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=inflater.inflate(R.layout.item_list, parent, false);

        RecyclerViewHolder viewHolder=new RecyclerViewHolder(v);
        return viewHolder;
    }

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

        holder.tv1.setText(name[position]);
        holder.imageView.setOnClickListener(clickListener);
        holder.imageView.setTag(holder);
    }

    View.OnClickListener clickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            RecyclerViewHolder vholder = (RecyclerViewHolder) v.getTag();
            int position = vholder.getPosition();

            Toast.makeText(context,"This is position "+position,Toast.LENGTH_LONG ).show();

        }
    };



    @Override
    public int getItemCount() {
        return name.length;
    }
}

RecyclerViewHolder.java

package androidwarriors.recycleviewdemo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

    TextView tv1,tv2;
    ImageView imageView;

    public RecyclerViewHolder(View itemView) {
        super(itemView);

        tv1= (TextView) itemView.findViewById(R.id.list_title);
        tv2= (TextView) itemView.findViewById(R.id.list_desc);
        imageView= (ImageView) itemView.findViewById(R.id.list_avatar);

    }
}

2 个答案:

答案 0 :(得分:0)

在Adapter类中,为Recycler View项添加以下代码:

@Override  
    public void onBindViewHolder(final ViewHolder holder, int position) { 
         // holder.itemView is your RelativeLayout which is inside your cardview
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
               //do your further respective fragment activity task
            } 
        }); 
    } 

答案 1 :(得分:0)

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

    holder.tv1.setText(name[position]);
    holder.imageView.setOnClickListener(clickListener);
    holder.imageView.setTag(holder);
}

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //do your further respective fragment activity task
        switch(position)
        {
            case 0:
                Intent intent=new Intent(your RecyclerActivity Reference, OneFragment.class);
                (Your Activity Reference).startActivity(intent);
        }
    });