飞飞翼 于 2021-11-14 17:07:59 发布 633 收藏 分类专栏: Android 文章标签: 网络 android android studio 版权 Android 专栏收录该内容 31 篇文章0 订阅 订阅专栏 1.样式文件 activity_main(主页面文件) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" xmlns:app="http://schemas./apk/res-auto" xmlns:tools="http://schemas./tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/refreshLayout"> <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recy2" > </androidx.recyclerview.widget.RecyclerView> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </LinearLayout> news_item_one文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="9dp" > <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_margin="10dp" android:layout_height="wrap_content"> <TextView android:layout_width="212dp" android:layout_height="63dp" android:text="《天外世界:果根险境》9月9日上架 游戏将支持Xbox Series X专属强化" android:id="@+id/title" android:layout_weight="1" android:textColor="@color/black" ></TextView> <ImageView android:layout_width="108dp" android:layout_height="53dp" android:id="@+id/thumb" android:scaleType="fitXY" android:src="@mipmap/news_bg" ></ImageView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" > <ImageView android:layout_width="16dp" android:layout_height="16dp" android:id="@+id/header" ></ImageView> <TextView android:layout_width="71dp" android:layout_height="17dp" android:text="游戏早知道 ." android:textColor="#777777" android:textSize="13sp" android:gravity="left" android:id="@+id/author" /> <TextView android:layout_width="39dp" android:layout_height="17dp" android:text="1评论 ." android:textColor="#777777" android:textSize="13sp" android:gravity="left" android:id="@+id/comment" /> <TextView android:layout_width="46dp" android:layout_height="17dp" android:text="1小时前" android:textColor="#777777" android:textSize="13sp" android:gravity="left" android:id="@+id/time" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#f5f5f5" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" ></View> </LinearLayout> news_item_two文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="CS+吃鸡+守望先锋,真正的大杂烩!" android:textColor="#222222" android:textSize="16sp" android:gravity="left" android:id="@+id/title" android:layout_margin="10dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="116dp" android:layout_height="78dp" android:layout_weight="1" android:id="@+id/pic1" android:src="@mipmap/news_bg" ></ImageView> <ImageView android:layout_width="116dp" android:layout_height="78dp" android:layout_weight="1" android:id="@+id/pic2" android:src="@mipmap/news_bg" ></ImageView> <ImageView android:layout_width="116dp" android:layout_height="78dp" android:layout_weight="1" android:id="@+id/pic3" android:src="@mipmap/news_bg" ></ImageView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" > <ImageView android:layout_width="16dp" android:layout_height="16dp" android:id="@+id/header" ></ImageView> <TextView android:layout_width="71dp" android:layout_height="17dp" android:text="游戏早知道 ." android:textColor="#777777" android:textSize="13sp" android:id="@+id/author" android:gravity="left" /> <TextView android:layout_width="39dp" android:layout_height="17dp" android:text="1评论 ." android:textColor="#777777" android:textSize="13sp" android:gravity="left" android:id="@+id/comment" /> <TextView android:layout_width="46dp" android:layout_height="17dp" android:text="1小时前" android:textColor="#777777" android:textSize="13sp" android:id="@+id/time" android:gravity="left" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#f5f5f5" android:layout_marginTop="0dp" android:layout_marginBottom="10dp" ></View> </LinearLayout> news_item_three <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas./apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_margin="10dp" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CS+吃鸡+守望先锋,真正的大杂烩!" android:textColor="#222222" android:textSize="16sp" /> <ImageView android:id="@+id/thumb" android:layout_width="match_parent" android:layout_height="190dp" android:layout_marginTop="7dp" android:scaleType="fitXY" android:src="@mipmap/news_bg" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="9dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/header" android:layout_width="16dp" android:layout_height="16dp" android:scaleType="fitXY" /> <TextView android:id="@+id/author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="9dp" android:text="游戏早知道 ." android:textColor="#777777" android:textSize="13sp" /> <TextView android:id="@+id/comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="9dp" android:text="1评论 ." android:textColor="#777777" android:textSize="13sp" /> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="9dp" android:text="1小时前" android:textColor="#777777" android:textSize="13sp" /> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#f5f5f5" /> </RelativeLayout> 2.适配器 package com.example.recymorestyleapplication.adapter; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.example.recymorestyleapplication.R; import com.example.recymorestyleapplication.view.CircleTransform; import com.squareup.picasso.Picasso; import java.util.ArrayList; import java.util.List; import java.util.Map; /** *首页视频适配器 **/ public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<String> list; private String TAG=""; public void setDatas(List<String> datas){ this.list=datas; } //构造器只需要传入界面上下文就行 public NewsAdapter(Context context){ this.context=context; this.list=list; } public NewsAdapter(Context context, List<String> list){ this.context=context; this.list=list; } //可以实现多种不同布局的 // position 数据下标 @Override public int getItemViewType(int position) { int a =position%3+1; return a; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (viewType==1){ ViewHolderOne viewHolderOne; View view= LayoutInflater.from(context).inflate(R.layout.news_item_one,parent,false); viewHolderOne=new ViewHolderOne(view); return viewHolderOne; }else if(viewType==2){ ViewHolderTwo viewHolderTwo; View view= LayoutInflater.from(context).inflate(R.layout.news_item_two,parent,false); viewHolderTwo=new ViewHolderTwo(view); return viewHolderTwo; }else { ViewHolderThree viewHolderThree; View view= LayoutInflater.from(context).inflate(R.layout.news_item_three,parent,false); viewHolderThree=new ViewHolderThree(view); return viewHolderThree; } } //绑定数据用的 给布局渲染数据 @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { int type = position%3+1; Log.d(TAG, "onBindViewHolder: 下标----"+type); if (type==1){ ViewHolderOne vho=(ViewHolderOne) holder; String author="作者1"; String title="标题1"; String comment="新闻内容"; String time="2020-08-08"; String headerUrl="http://crawl.ws.126.net/img/4ea8924e432bc4067a1d2f8bb90e8793.jpg"; vho.author.setText(author); vho.comment.setText(comment); vho.time.setText(time); vho.title.setText(title); Picasso.get() .load(headerUrl) .transform( new CircleTransform())//圆头像 .into( vho.header); Picasso.get() .load(headerUrl) .into( vho.thumb); }else if(type==2){ ViewHolderTwo vho=(ViewHolderTwo) holder; String author="作者1"; String title="标题1"; String comment="新闻内容"; String time="2020-08-08"; String headerUrl="http://crawl.ws.126.net/img/4ea8924e432bc4067a1d2f8bb90e8793.jpg"; vho.author.setText(author); vho.comment.setText(comment); vho.time.setText(time); vho.title.setText(title); Picasso.get() .load(headerUrl) .transform(new CircleTransform())//圆头像 .into( vho.header); Picasso.get() .load(headerUrl) .into( vho.pic1); Picasso.get() .load(headerUrl) .into( vho.pic2); Picasso.get() .load(headerUrl) .into( vho.pic3); }else { ViewHolderThree vho=(ViewHolderThree) holder; String author="作者1"; String title="标题1"; String comment="新闻内容"; String time="2020-08-08"; String headerUrl="http://crawl.ws.126.net/img/4ea8924e432bc4067a1d2f8bb90e8793.jpg"; vho.author.setText(author); vho.comment.setText(comment); vho.time.setText(time); vho.title.setText(title); Picasso.get() .load(headerUrl) .transform(new CircleTransform())//圆头像 .into( vho.header); Picasso.get() .load(headerUrl) .into( vho.thumb); } } public static <T> List<T> castList(Object obj, Class<T> clazz) { List<T> result = new ArrayList<T>(); if(obj instanceof List<?>) { for (Object o : (List<?>) obj) { result.add(clazz.cast(o)); } return result; } return null; } // 当前多少个数据 @Override public int getItemCount() { if(list!=null&&list.size()>0){ Log.d(TAG, "getItemCount: 数量"+list.size()); return list.size(); }else{ return 0; } } public class ViewHolderOne extends RecyclerView.ViewHolder { private TextView title; private TextView author; private TextView comment; private TextView time; private ImageView header; private ImageView thumb; private Map<String,Object> newsEntity; public ViewHolderOne(@NonNull View view) { super(view); title = view.findViewById(R.id.title); author = view.findViewById(R.id.author); comment = view.findViewById(R.id.comment); time = view.findViewById(R.id.time); header = view.findViewById(R.id.header); thumb = view.findViewById(R.id.thumb); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick: 点击数还记3"); } }); } } public class ViewHolderTwo extends RecyclerView.ViewHolder { private TextView title; private TextView author; private TextView comment; private TextView time; private ImageView header; private ImageView pic1, pic2, pic3; private Map<String,Object> newsEntity; public ViewHolderTwo(@NonNull View view) { super(view); title = view.findViewById(R.id.title); author = view.findViewById(R.id.author); comment = view.findViewById(R.id.comment); time = view.findViewById(R.id.time); header = view.findViewById(R.id.header); pic1 = view.findViewById(R.id.pic1); pic2 = view.findViewById(R.id.pic2); pic3 = view.findViewById(R.id.pic3); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick: 点击了"); } }); } } public class ViewHolderThree extends RecyclerView.ViewHolder { private TextView title; private TextView author; private TextView comment; private TextView time; private ImageView header; private ImageView thumb; private Map<String,Object> newsEntity; public ViewHolderThree(@NonNull View view) { super(view); title = view.findViewById(R.id.title); author = view.findViewById(R.id.author); comment = view.findViewById(R.id.comment); time = view.findViewById(R.id.time); header = view.findViewById(R.id.header); thumb = view.findViewById(R.id.thumb); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick: 点击数还记2"); } }); } } } 3.图片圆 实体类 import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Paint; import com.squareup.picasso.Transformation; /** * @author: 源头像 * @date: 2020-07-07 **/ public class CircleTransform implements Transformation { public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); if (squaredBitmap != source) { source.recycle(); } Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); paint.setShader(shader); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); squaredBitmap.recycle(); return bitmap; } public String key() { return "circle"; } } 4.主activity package com.example.recymorestyleapplication; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import android.os.Handler; import android.os.Message; import com.example.recymorestyleapplication.adapter.NewsAdapter; import com.scwang.smartrefresh.layout.api.RefreshLayout; import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener; import com.scwang.smartrefresh.layout.listener.OnRefreshListener; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; import java.util.Map; public class MainActivity extends BaseActivity { private RecyclerView recyclerView; private RefreshLayout refreshLayout; private LinearLayoutManager linearLayoutManager; private NewsAdapter newsAdapter; private List<String> list=new ArrayList<>();//视频列表 private String TAG="新闻列表"; private Handler mhandler=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); //放在这里面就在UI朱线程里面执行 switch (msg.what){ case 0: newsAdapter.setDatas(list); newsAdapter.notifyDataSetChanged();//通知recyview刷新页面 break; case 1: break; } } }; @Override protected int initLayout() { return R.layout.activity_main; } @Override protected void initView() { recyclerView =findViewById(R.id.recy2); refreshLayout=findViewById(R.id.refreshLayout);//上拉刷新 } @Override protected void initData() { linearLayoutManager=new LinearLayoutManager(MainActivity.this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//垂直排列 recyclerView.setLayoutManager(linearLayoutManager); newsAdapter=new NewsAdapter(MainActivity.this); recyclerView.setAdapter(newsAdapter);//绑定适配器 //下拉刷新第三方插件) refreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { getNewsList(true); } }); // 上拉加载 refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore(RefreshLayout refreshlayout) { getNewsList(false); } }); //获取新闻数据 getNewsList(true); getNewsList(true); } public void getNewsList(boolean isRefresh){ if (isRefresh) { refreshLayout.finishRefresh(true); } else { refreshLayout.finishLoadMore(true); } if(isRefresh) { list=new ArrayList<>(); for (int a=0;a<3;a++){ list.add(a+""); } }else { for (int a=0;a<3;a++){ list.add(a+""); } } mhandler.sendEmptyMessage(0); } } 公共activity import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Looper; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; /** *基类 公众的方法 * * abstract 抽象类 **/ public abstract class BaseActivity extends AppCompatActivity { private Context mcontext; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mcontext=this; setContentView(initLayout()); initView(); initData(); } protected abstract int initLayout(); protected abstract void initView(); protected abstract void initData(); public void ShowToast(String msg){ Toast.makeText(mcontext,msg,Toast.LENGTH_SHORT).show(); } public void ShowToastSync(String msg){ Looper.prepare();//消息队列自学 Toast.makeText(mcontext,msg,Toast.LENGTH_SHORT).show(); Looper.loop(); } public void navigateTo(Class cls){ Intent i=new Intent(this, cls); startActivity(i); } protected void saveStringToSp(String key,String val){ SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit(); editor.putString(key,val); editor.commit(); } } ———————————————— 版权声明:本文为CSDN博主「飞飞翼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_36935391/article/details/121320072 |
|