在開始之前呢想必大家對列表都遇到很多各式不同的需求,具體看大家怎麼習慣用哪個控件了、
一:效果圖:
二:實現步驟:
(A)RecyclerView控件
我這呢用的SmartRefreshLayout+RecyclerView來實現的,因爲需要分頁加載,差不多都有註釋就不用多解釋什麼了,直接上代碼、
1:引用刷新框架
//刷新框架
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5'
2:layout佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/titlelayout" />
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/fbrefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/view_id"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#fff"
app:srlEnableLoadMoreWhenContentNotFull="false">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlAccentColor="#393939" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"></android.support.v7.widget.RecyclerView>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlAccentColor="#393939" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
3:activity實現
(1):定義變量
private SmartRefreshLayout fbrefresh;//刷新
private int pageNum = 1;//頁碼
private RecyclerView recyclerview;//列表
private IncentiveActivitiesAdapter adapter;//adapter
(2):實例化
/**
* 實例化
*/
private void instantiation() {
relativefh = findViewById(R.id.relativefh);
title = findViewById(R.id.title);
title.setText("獎勵活動");
fbrefresh = findViewById(R.id.fbrefresh);
//刷新
fbrefresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
pageNum = 1;
rightDatelist();
}
});
//加載
fbrefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(final RefreshLayout refreshLayout) {
pageNum++;
// rightDatelist();
}
});
recyclerview = findViewById(R.id.recyclerview);
recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));//橫向顯示
recyclerview.addItemDecoration(new SpacesItemDecoration(0));
relativefh.setOnClickListener(this);
}
(3):網絡解析數據
/**
* 獎勵活動列表 post 請求
* (需要把參數放到request Body裏面,並且在在header裏面申明請求類型爲“application/json”)
*/
private void rightDatelist() {
String url = NetConst.JLLB;
System.out.println("====>>獎勵活動列表url:" + url);
JSONObject jsonObjectTemp = new JSONObject();
try {
jsonObjectTemp.put("appid", "com.hsrd.highlandwind");
jsonObjectTemp.put("type", "1");
} catch (JSONException e) {
e.printStackTrace();
}
String json = jsonObjectTemp.toString();
System.out.println("獎勵活動列表post傳json:" + json);
//獲取網絡工具類實例
Okhttp3Utils netUtils = Okhttp3Utils.getInstance();
netUtils.doPostJson(url, json, new Okhttp3Utils.MyNetCall() {
@Override
public void success(Call call, Response response) throws IOException {
try {
String star = response.body().string();
System.out.println("===>>獎勵活動列表成功:" + star);
/**把data下的數據轉換成json對象**/
final JSONObject jDat = new JSONObject(star);
if (!jDat.getString("code").equals("0")) {
// 加載完數據之後,啓動滑動
Message msg = new Message();
msg.what = 2;
handler.sendMessage(msg);
}
if (jDat.getString("code").equals("0")) {
bean = (List<IncentiveBean>) JsonCllUtil.parseJsonToListint(jDat.getString("data"), new
TypeToken<List<IncentiveBean>>() {
}.getType());
// 加載完數據之後,啓動滑動
Message msg = new Message();
if (pageNum == 1) {//1爲刷新,3爲加載
msg.what = 1;
} else {
msg.what = 3;
}
handler.sendMessage(msg);
} else {
// 加載完數據之後,啓動滑動
Message msg = new Message();
msg.what = 2;
handler.sendMessage(msg);
}
} catch (Exception e) {
}
}
@Override
public void failed(Call call, IOException e) {
// 加載完數據之後,啓動滑動
Message msg = new Message();
msg.what = 2;
handler.sendMessage(msg);
}
});
}
(4):得到數據賦值到adapter
@SuppressLint("HandlerLeak")
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//刷新
case 1:
adapter = new IncentiveActivitiesAdapter(bean, IncentiveActivities.this);
recyclerview.setAdapter(adapter);
fbrefresh.finishRefresh();
break;
//加載
case 3:
adapter.setData(bean);
fbrefresh.finishLoadMore();
break;
}
}
};
4:adapter
package pinbidarider.hsrd.com.pinbidarider.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
import pinbidarider.hsrd.com.pinbidarider.R;
import pinbidarider.hsrd.com.pinbidarider.activity.AwardDetails;
import pinbidarider.hsrd.com.pinbidarider.model.IncentiveBean;
/**
* 作者:CaoLiulang
* ❤
* Date:2020/5/12
* ❤
* 模塊:活動列表adapter
*/
public class IncentiveActivitiesAdapter extends RecyclerView.Adapter<IncentiveActivitiesAdapter.ViewHolder> {
private List<IncentiveBean> list;
private Context context;
private IsetOnc isetonc;
public IncentiveActivitiesAdapter(List<IncentiveBean> list, Context context) {
this.list = list;
this.context = context;
}
/**
* 加載更多
*
* @param mPageList
*/
public void setData(List<IncentiveBean> mPageList) {
try {
if (mPageList != null) {
int previousSize = 0;
try {
previousSize = list.size();
} catch (Exception e) {
previousSize = 0;
}
int sizez = previousSize + 2;
list.addAll(mPageList);
notifyItemRangeInserted(sizez, mPageList.size());
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.incentive_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
/**
* 類似GetView
*
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.textcdje.setText(list.get(position).name);//衝單金額
holder.textkdje.setText("預計可得:¥" + list.get(position).money);//可得金額
holder.texytgz.setText("獎勵規則:" + list.get(position).remarks);//獎勵規則
//點擊事件
holder.relative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(new Intent(context, AwardDetails.class).putExtra("id", list.get(position).id));
}
});
//setTag當前的TAG
holder.itemView.setTag(position);
}
/**
* 長度
*
* @return
*/
@Override
public int getItemCount() {
return list.size();
}
/**
* 初始化組件
*/
class ViewHolder extends RecyclerView.ViewHolder {
TextView textcdje, textkdje, texytgz;
RelativeLayout relative;
public ViewHolder(final View itemView) {
super(itemView);
textcdje = itemView.findViewById(R.id.textcdje);
textkdje = itemView.findViewById(R.id.textkdje);
texytgz = itemView.findViewById(R.id.texytgz);
relative = itemView.findViewById(R.id.relative);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isetonc != null) {
//點擊id時候判斷如果isetonc不爲空的時候再給接口賦值,這樣就不會出現空指針了
//獲取之前setTag的值,給接口賦值
isetonc.setOnClickList((Integer) itemView.getTag());
}
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (isetonc != null) {
//點擊id時候判斷如果isetonc不爲空的時候再給接口賦值,這樣就不會出現空指針了
isetonc.setOnLongClickList((Integer) itemView.getTag());
}
return true;
}
});
}
}
/**
* 給接口賦值
*
* @param isetonc
*/
public void setOnItem(IsetOnc isetonc) {
this.isetonc = isetonc;
}
interface IsetOnc {
//點擊事件
void setOnClickList(int xiabiao);
//長按事件
void setOnLongClickList(int xiabiao);
}
}
(5):item佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/texttop"
android:layout_width="match_parent"
android:layout_height="10dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_below="@+id/texttop"
android:src="@mipmap/jlhdback" />
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/texttop"
android:layout_margin="15dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/jlhdback"
android:text="獎勵活動"
android:textColor="#ffffff"
android:textSize="10dp" />
<TextView
android:id="@+id/textcdje"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="0元衝單獎勵"
android:textColor="#fff"
android:textSize="16dp" />
<TextView
android:id="@+id/textkdje"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="預計可得¥0"
android:textColor="#000"
android:textSize="14dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/linear"
android:layout_marginLeft="3.5dp"
android:layout_marginTop="-10dp"
android:layout_marginRight="3.5dp"
android:background="#fff">
<TextView
android:id="@+id/texytgz"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/image"
android:text="獎勵規則:。。。。。。。。。。。。"
android:textSize="12dp" />
<ImageView
android:id="@+id/image"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:src="@mipmap/submit_address_right" />
</RelativeLayout>
</RelativeLayout>
(B)ListView控件
1:layout佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/titlelayout" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:divider="#00000000"
android:visibility="gone"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"/>
</LinearLayout>
2:activity(這是listview動態添加數據,我一般是後臺數據沒出來用這個看效果而已,都很少用listview了,都是用RecyclerView,item佈局是上面那個item佈局)
private ListView listview;
// //listview動態添加數據
// ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
// for (int i = 1; i < 5; i++) {
// HashMap<String, Object> map = new HashMap<String, Object>();
// map.put("ItemTitle", i+"元衝單");
// listItem.add(map);
// }
// //生成適配器的Item和動態數組對應的元素
// SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,//數據源
// R.layout.listview_itempw,
// //動態數組與ImageItem對應的子項
// new String[]{"ItemTitle"},
// //ImageItem的XML文件裏面的一個ImageView,兩個TextView ID
// new int[]{R.id.textsc}
// );
// //添加並且顯示
// listview_zy.setAdapter(listItemAdapter2);
// listview_zy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//// startActivity(new Intent(MessageCenter.this, NewsDetails.class));
// }
// });
// setListViewHeightBasedOnChildren(listview_pw);
(C):LinearLayout控件
有時候我們在詳情頁會遇到時間呀,價格呀這些列表,如果裏面嵌套了ScollView,又有RecyclerView的話再嵌套listview就比較麻煩,所以直接用LinearLayout,看看效果圖吧
1:layout佈局直接添加一個LinearLayout
<LinearLayout
android:id="@+id/linearlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
2:activity使用
(1):定義
private LinearLayout linearlist,linearlist1;
(2):實例化
linearlist = findViewById(R.id.linearlist);
linearlist1 = findViewById(R.id.linearlist1);
(3):使用(使用前一定要清空一下)
//清空View
linearlist.removeAllViews();
for (int i = 0; i < bean.holiday_condition.size(); i++) {
LinearLayout flview = (LinearLayout)
LayoutInflater.from(AwardDetails.this).inflate(R.layout.biaoqian_tiem, null);
final TextView textv = flview.findViewById(R.id.textv);
textv.setText(bean.holiday_condition.get(i));
linearlist.addView(flview);
}
//清空View
linearlist1.removeAllViews();
for (int i = 0; i < bean.time_condition.size(); i++) {
LinearLayout flview = (LinearLayout) LayoutInflater.from(AwardDetails.this).inflate(R.layout.biaoqian_tiem, null);
final TextView textv = flview.findViewById(R.id.textv);
textv.setText(bean.time_condition.get(i));
linearlist1.addView(flview);
}
(4):Javabean
package pinbidarider.hsrd.com.pinbidarider.model;
import java.util.List;
/**
* 作者:CaoLiulang
* ❤
* Date:2020/5/12
* ❤
* 模塊:活動詳情Javabean
*/
public class AwardDetailsBean {
public String id;
public String name;
public String money;
public String status;
public String remarks;
public String rider_condition;
public String region_condition;
public List<String> holiday_condition;
public String industry_condition;
public String store_condition;
public List<String> time_condition;
@Override
public String toString() {
return "AwardDetailsBean{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", money='" + money + '\'' +
", status='" + status + '\'' +
", remarks='" + remarks + '\'' +
", rider_condition='" + rider_condition + '\'' +
", region_condition='" + region_condition + '\'' +
", holiday_condition=" + holiday_condition +
", industry_condition='" + industry_condition + '\'' +
", store_condition='" + store_condition + '\'' +
", time_condition=" + time_condition +
'}';
}
}
---------------------到這就結束了,大神別噴,初學者可以借鑑,歡迎各位指出問題所在!