先导入Goodview包链接在上一篇中。只要最上面的goodview。不要别的sample的。
然后就是导入;
File->new->import Moudle(第五个)
2.app右击->(倒数第四个)open moudle setting
3.(左)app,(上)dependencies.点击+号,选择第三个Moudle dependencies.就可以看到你刚刚添加的goodview。
最后;在gradle app中添加 (dependencies中)
implementation project(’:goodview’)
cardview.xml中要添加的东西:
在第二个LinearLayout中添加以下代码
<ImageView
android:id="@+id/news_good"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/collection"
android:layout_marginLeft="250dp"
android:onClick="collection"//在XML文件中显式指定控件的onClick属性,点击按钮时会利用反射的方
//式调用对应Activity中的onClick()方法。
android:clickable="true"/>
User.java中
还是提供完整的吧,在注释里进行说明。
public class User {
private String news_title;
private String news_info;
private int news_thumbid;
private int news_goodid;//添加
public User(String news_title, String news_info, int news_thumbid,int news_good) {
this.news_title = news_title;
this.news_info = news_info;
this.news_thumbid = news_thumbid;
this.news_goodid=news_good;//添加
}
public int getNews_good() {//添加
return news_goodid;
}
public void setNews_good(int news_good) {
this.news_goodid = news_good;//添加
}
public String getNews_title() {
return news_title;
}
public void setNews_title(String news_title) {
this.news_title = news_title;
}
public String getNews_info() {
return news_info;
}
public void setNews_info(String news_info) {
this.news_info = news_info;
}
public int getNews_thumbid() {
return news_thumbid;
}
public void setNews_thumbid(int news_thumbid) {
this.news_thumbid = news_thumbid;
}
}
mainActivity.java 中
public class MainActivity extends AppCompatActivity {
List<User> data;
public GoodView mGoodView;//添加
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView =findViewById(R.id.news_list);
mGoodView = new GoodView(this);//添加初始话
data= new ArrayList<>();
data.add(new User("毡帽系列","此系列服装有点cute,像不像小车夫。",R.drawable.i1,R.mipmap.collection));//R.drawable.collection是Goodview中提供的
data.add(new User("蜗牛系列","宝宝变成了小蜗牛,爬啊爬啊爬啊。",R.drawable.i2,R.mipmap.collection));//R.drawable.collection是Goodview中提供的
data.add(new User("小蜜蜂系列","小蜜蜂,小蜜蜂,飞到西,飞到东。",R.drawable.i3,R.mipmap.collection));//R.drawable.collection是Goodview中提供的
data.add(new User("毡帽系列","此系列服装有点cute,像不像小车夫。",R.drawable.i4,R.mipmap.collection));//R.drawable.collection是Goodview中提供的
data.add(new User("蜗牛系列","宝宝变成了小蜗牛,爬啊爬啊爬啊。",R.drawable.i5,R.mipmap.collection));//R.drawable.collection是Goodview中提供的
data.add(new User("小蜜蜂系列","小蜜蜂,小蜜蜂,飞到西,飞到东。",R.drawable.i6,R.mipmap.collection));//R.drawable.collection是Goodview中提供的
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
NewsListAdapter adapter= new NewsListAdapter(mGoodView);
recyclerView.setAdapter(adapter);
}
public void collection(View view) {
((ImageView) view).setImageResource(R.mipmap.collection_checked);
mGoodView.setTextInfo("收藏成功", Color.parseColor("#f66467"), 12);
mGoodView.show(view);//R.drawable.collection_checked是Goodview中提供的
}//项目提供的方法
public void reset(View view) {
((ImageView) findViewById(R.id.news_good)).setImageResource(R.mipmap.collection);
mGoodView.reset();//项目提供的方法
}
class NewsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
GoodView mGoodView;
public NewsListAdapter(GoodView mGoodView){
this.mGoodView = mGoodView;//构造方法
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemview = LayoutInflater.from(MainActivity.this).from(viewGroup.getContext()).inflate(R.layout.cardview,viewGroup,false);
final ViewHolder holder=new ViewHolder(itemview);
holder.news_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
User user=(User) holder.itemView.getTag();
Toast.makeText(getApplicationContext(),user.getNews_title(), Toast.LENGTH_SHORT).show();
}
});
holder.news_info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
User user=(User) holder.itemView.getTag() ;
Toast.makeText(getApplicationContext(), user.getNews_info(), Toast.LENGTH_SHORT).show();
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
User user = data.get(position);
if (null==user)
return;
ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.news_title.setText(user.getNews_title());
viewHolder.news_good.setImageResource(user.getNews_good());
viewHolder.news_info.setText(user.getNews_info());
viewHolder.news_thumb.setImageResource(user.getNews_thumbid());
viewHolder.itemView.setTag(user);
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView news_title;
ImageView news_thumb;
TextView news_info;
ImageView news_good;
public ViewHolder(@NonNull View itemView) {
super(itemView);
news_good=itemView.findViewById(R.id.news_good);
news_title = itemView.findViewById(R.id.news_title);
news_thumb = itemView.findViewById(R.id.news_thumb);
news_info = itemView.findViewById(R.id.news_info);
itemView.findViewById(R.id.news_container).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
}
就这样完成了。
其他效果可参考Goodview项目。