1.mainactivity
public class MainActivity extends AppCompatActivity {
private ArrayList<RecycleUser.DataBean> list = new ArrayList<>();
private String path = "http://www.xieast.com/api/news/news.php?page=1";
private RecyclerView recycle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycle = findViewById(R.id.recycler);
Tools tools = new Tools();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//瀑布流
//StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
//staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.fen));
recycle.addItemDecoration(dividerItemDecoration);
recycle.setLayoutManager(linearLayoutManager);
tools.get(path).setData(new Tools.HttpData() {
@Override
public void getData(String s) {
Gson gson = new Gson();
RecycleUser user = gson.fromJson(s, RecycleUser.class);
list.addAll(user.getData());
runOnUiThread(new Runnable() {
@Override
public void run() {
final RecycleViewAdapter adapter = new RecycleViewAdapter(getApplicationContext(),list);
adapter.setRecycleOnClick(new RecycleViewAdapter.RecycleitemClick() {
@Override
public void onItemClick(View view, int position) {
//adapter.removeData(position);
//adapter.addData(position);
adapter.updateData(position);
Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
}
});
recycle.setAdapter(adapter);
}
});
}
});
}
}
2.适配器
public class RecycleViewAdapter extends RecyclerView.Adapter {
private Context mContext;
private ArrayList<RecycleUser.DataBean> list;
//3.全局变量
private RecycleitemClick click;
public RecycleViewAdapter(Context context, ArrayList<RecycleUser.DataBean> list) {
mContext = context;
this.list = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
View view;
RecyclerView.ViewHolder holder = null;
switch (position){
case 1:
view = LayoutInflater.from(mContext).inflate(R.layout.item, viewGroup, false);
//4.传入点击事件
holder = new MyViewHolder(view,click);
break;
case 2:
view = LayoutInflater.from(mContext).inflate(R.layout.item1, viewGroup, false);
holder = new MyViewHolder2(view,click);
break;
}
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
RecycleUser.DataBean bean = list.get(position);
switch (getItemViewType(position)){
case 1:
Picasso.with(mContext).load(bean.getThumbnail_pic_s()).into(((MyViewHolder) viewHolder).image);
((MyViewHolder) viewHolder).text.setText(bean.getAuthor_name());
break;
case 2:
Picasso.with(mContext).load(bean.getThumbnail_pic_s03()).into(((MyViewHolder2) viewHolder).image1);
Picasso.with(mContext).load(bean.getThumbnail_pic_s02()).into(((MyViewHolder2) viewHolder).image2);
((MyViewHolder2) viewHolder).text1.setText(bean.getAuthor_name());
break;
}
}
@Override
public int getItemViewType(int position) {
if(position%2!=0){
return 1;
}else{
return 2;
}
}
public void removeData(int position){
list.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
}
public void addData(int position){
list.add(position,list.get(position));
notifyItemInserted(position+1);
}
public void updateData(int position){
//list.add(position,list.get(position));
//notifyItemRangeChanged(position,1);
list.set(position,list.get(position));
notifyItemChanged(position);
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
//1.定义接口
public interface RecycleitemClick{
public void onItemClick(View view,int position);
}
//2.实例化接口
public void setRecycleOnClick(RecycleitemClick onclick){
this.click = onclick;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView text;
ImageView image;
public MyViewHolder(@NonNull View itemView, final RecycleitemClick click) {
super(itemView);
text = itemView.findViewById(R.id.text1);
image = itemView.findViewById(R.id.image1);
//5.拿到点击事件
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click.onItemClick(v,getAdapterPosition());
}
});
}
}
class MyViewHolder2 extends RecyclerView.ViewHolder{
TextView text1;
ImageView image1;
ImageView image2;
public MyViewHolder2(@NonNull View itemView, final RecycleitemClick click) {
super(itemView);
text1 = itemView.findViewById(R.id.text2);
image1 = itemView.findViewById(R.id.image2);
image2 = itemView.findViewById(R.id.image3);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click.onItemClick(v,getAdapterPosition());
}
});
}
}
}
3.工具类
public class Tools {
public Tools(){}
public Tools get(String path){
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.writeTimeout(3000,TimeUnit.MILLISECONDS);
builder.readTimeout(3000,TimeUnit.MILLISECONDS);
StringBuilder stringBuilder = new StringBuilder();
String params = stringBuilder.toString();
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json charset=utf-8"), params);
Request request = new Request.Builder()
//.get()
.post(requestBody)
.url(path)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
int code = response.code();
if(code==200){
ResponseBody body = response.body();
String string = body.string();
data.getData(string);
}
}
});
return this;
}
private HttpData data;
public void setData(HttpData data){
this.data = data;
}
public interface HttpData{
void getData(String s);
}
}
3.user类
4.Androidmanifest
<uses-permission android:name="android.permission.INTERNET"/>
recyclerview多条目
最新推荐文章于 2021-08-05 17:21:20 发布