效果实现图
使用的依赖和权限
implementation 'com.facebook.fresco:fresco:0.12.0' implementation
'com.squareup.okhttp3:okhttp:3.4.2'implementation
'com.google.code.gson:gson:2.2.4'implementation
'com.android.support:recyclerview-v7:27.1.1'implementation
'com.android.support:support-v4:27.1.1'<uses-permission android:name=
"android.permission.INTERNET"/>activity_questions
<?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" xmlns:tools=
"http://schemas.android.com/tools" android:layout_width="match_parent" android
:layout_height="match_parent" android:orientation="vertical" tools:context=
".QuestionsActivity"> <LinearLayout android:layout_width="match_parent" android
:layout_height="wrap_content" android:orientation="horizontal"> <Button android
:layout_width="0dp" android:layout_weight="1" android:layout_height=
"wrap_content" android:text="线性布局" android:id="@+id/ll"/> <Button android
:layout_width="0dp" android:layout_weight="1" android:layout_height=
"wrap_content" android:text="表格布局" android:id="@+id/bg"/> </LinearLayout> <
android.support.v7.widget.RecyclerView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/rlv"/> </LinearLayout>item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"> <
com.facebook.drawee.view.SimpleDraweeView android:id="@+id/iv" android
:layout_width="match_parent" android:layout_height="300dp" /> <LinearLayout
android:orientation="vertical" android:layout_width="match_parent" android
:layout_height="wrap_content"> <TextView android:id="@+id/textView" android
:textSize="20dp" android:layout_width="wrap_content" android:layout_height=
"wrap_content" android:text="商品信息:" /> <TextView android:id="@+id/tv" android
:layout_width="match_parent" android:layout_height="wrap_content" android:text=
"tv"/> <LinearLayout android:orientation="horizontal" android:layout_width=
"match_parent" android:layout_height="wrap_content"> <TextView android:id=
"@+id/textView2" android:textSize="20dp" android:layout_width="80dp" android
:layout_height="wrap_content" android:text="价格:" /> <TextView android:id=
"@+id/tv2" android:textSize="20dp" android:textColor="#f00" android
:layout_width="wrap_content" android:layout_height="wrap_content" android:text=
"tv2"/> </LinearLayout> </LinearLayout> </LinearLayout>
QuestionsActivity
import android.os.Handler; import android.os.Message; import
android.support.v7.app.AppCompatActivity;import android.os.Bundle; import
android.support.v7.widget.GridLayoutManager;import
android.support.v7.widget.LinearLayoutManager;import
android.support.v7.widget.RecyclerView;import android.view.View; import
android.widget.Button;import com.bwie.test.adapter.RecyclerviewAdapter; import
com.bwie.test.bean.Bean;import com.google.gson.Gson; import java.io.IOException;
importjava.util.ArrayList; import java.util.List; import okhttp3.OkHttpClient;
importokhttp3.Request; import okhttp3.Response; public class QuestionsActivity
extendsAppCompatActivity implements View.OnClickListener{ private static final
intSUCCESS = 492; List<Bean.JsonBean> datas = new ArrayList<>(); String path =
"http://p6acqdvet.bkt.clouddn.com/_data_json.json"; Handler handler = new
Handler() {@Override public void handleMessage(Message msg) { super
.handleMessage(msg);switch (msg.what) { case SUCCESS: String tvv = (String)
msg.obj; Gson gson =new Gson(); Bean bean = gson.fromJson(tvv, Bean.class);
List<Bean.JsonBean> data = bean.getJson(); datas.addAll(data); } } };private
RecyclerviewAdapter adapter;private RecyclerView mRlv; private Button ll;
privateButton bg; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_questions);
//隐藏原有标题 getSupportActionBar().hide(); initadd();//初始化 getOk(); } private void
initadd() {mRlv = (RecyclerView) findViewById(R.id.rlv); ll = findViewById(R.id.
ll);//线性布局 bg = findViewById(R.id.bg);//表格布局 //按钮的监听 ll.setOnClickListener(this
);bg.setOnClickListener(this); } @Override public void onClick(View v) { switch
(v.getId()){case R.id.ll: //线性布局管理器 mRlv.setLayoutManager(new
LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); setadapter();
break; case R.id.bg: //表格布局管理器 mRlv.setLayoutManager(new GridLayoutManager(this,
2)); setadapter(); break; } } private void setadapter() { adapter = new
RecyclerviewAdapter(this, datas); mRlv.setAdapter(adapter);
adapter.SetOnItemClickListener(new RecyclerviewAdapter.OnItemClickListener() {
@Overridepublic void onItemClick(int position) { adapter.swipe(position); } });
}private void getOk() { new Thread() { @Override public void run() { super
.run();//请求网络得到相应对象 OkHttpClient client = new OkHttpClient.Builder().build();
Request request =new Request.Builder().url(path).build(); try { Response
response = client.newCall(request).execute();//得到数据并转型 String s =
response.body().string();//更新UI不能在子线程中,使用Handler Message message =
handler.obtainMessage(); message.obj = s; message.what = SUCCESS;
handler.sendMessage(message); }catch (IOException e) { e.printStackTrace(); } }
}.start();return; } }
app文件夹下MyApp
import android.app.Application; import
com.facebook.drawee.backends.pipeline.Fresco;public class MyApplication extends
Application{@Override public void onCreate() { super.onCreate(); //初始化fresco
Fresco.initialize(this); } }
inter文件夹下ItemTouchCallBack
import android.support.v7.widget.RecyclerView; import
android.support.v7.widget.helper.ItemTouchHelper;import
com.bwie.test.adapter.RecyclerviewAdapter;public class ItemTouchCallBack extends
ItemTouchHelper.Callback{private RecyclerviewAdapter myAdapter; public
ItemTouchCallBack(RecyclerviewAdapter myAdapter) {this.myAdapter = myAdapter; }
/** * 获取移动的标记 移动和删除 * @param recyclerView * @param viewHolder * @return */
@Override public int getMovementFlags(RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder) {//设置移动的标记 int dragFlag=ItemTouchHelper.UP
|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT; //设置删除的标记 int
swipFlag=ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT; //制作两种标记 int i =
ItemTouchHelper.Callback.makeMovementFlags(dragFlag, swipFlag); return i; } /**
*@param recyclerView * @param viewHolder 原先的viewHolder * @param target
拖动后的viewHolder * @return */ @Override public boolean onMove(RecyclerView
recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder
target) {//切换位置 int oldPosition = viewHolder.getAdapterPosition(); int
newPosition = target.getAdapterPosition();//交换位置-- myAdapter
.onMove(oldPosition,newPosition);return true; } /** * 左右移动 * @param viewHolder
*@param direction */ @Override public void onSwiped(RecyclerView.ViewHolder
viewHolder,int direction) { int adapterPosition =
viewHolder.getAdapterPosition();myAdapter.swipe(adapterPosition); } }
MoveSwipeListener
public interface MoveSwipeListener { void onMove(int oldPosition, int
newPosition);void swipe(int position); }
adapter文件夹下RecyclerviewAdapter
import android.content.Context; import android.content.DialogInterface; import
android.net.Uri;import android.support.annotation.NonNull; import
android.support.v7.app.AlertDialog;import
android.support.v7.widget.RecyclerView;import android.view.View; import
android.view.ViewGroup;import android.widget.AdapterView.OnItemClickListener;
importandroid.widget.ImageView; import android.widget.LinearLayout; import
android.widget.TextView;import android.widget.Toast; import
com.bwie.test.bean.Bean;import com.bwie.test.inter.MoveSwipeListener; import
com.bwie.test.mytestquestions.R;import java.util.List; public class
RecyclerviewAdapterextends RecyclerView.Adapter implements MoveSwipeListener{
privateContext context; private List<Bean.JsonBean> datas; private
OnItemClickListeneronItemClickListener; public RecyclerviewAdapter(Context
context, List<Bean.JsonBean> datas) {this.context = context; this.datas =
datas; }@NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(
@NonNullViewGroup parent, int viewType) { View view = LinearLayout.inflate(
context, R.layout.item, null); MyViewHolder vh = new MyViewHolder(view); return
vh; }@Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder
holder,final int position) { MyViewHolder vh = (MyViewHolder) holder; vh.tv
.setText(datas.get(position).getName()); vh.tv2.setText(datas
.get(position).getAgio()); vh.iv.setImageURI(Uri.parse(datas
.get(position).getImage()));//
ImageLoader.getInstance().displayImage(datas.get(position).getImage(),vh.iv, //
ImageloaderUtil.getImageOptions()); // final String picUrl =
datas.get(position).getImage();// final Uri uri = Uri.parse(picUrl); //
vh.iv.setImageURI(uri); vh.itemView.setOnClickListener(new
View.OnClickListener() {@Override public void onClick(View v) { if (
onItemClickListener!= null){ onItemClickListener.onItemClick(position); } /**
* 点击条目删除 */ AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("我是对话框");
builder.setMessage("我是对话框的内容"); builder.setNegativeButton("取消", new
DialogInterface.OnClickListener() {@Override public void
onClick(DialogInterface dialog,int which) { Toast.makeText(context, "点击了取消按钮",
Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.setPositiveButton(
"确定", new DialogInterface.OnClickListener() { @Override public void
onClick(DialogInterface dialog,int which) { Toast.makeText(context, "点击了确定的按钮",
Toast.LENGTH_SHORT).show(); dialog.dismiss(); //移除数据 datas.remove(position);
//条目刷新 notifyDataSetChanged(); } }); AlertDialog dialog = builder.create();
dialog.show(); } }); }@Override public int getItemCount() { return datas
.size(); }@Override public void onMove(int oldPosition, int newPosition) { }
@Override public void swipe(int position) { } public interface
OnItemClickListener {void onItemClick(int position); } public void
SetOnItemClickListener(OnItemClickListener onItemClickListener){this.
onItemClickListener= onItemClickListener; } private class MyViewHolder extends
RecyclerView.ViewHolder {private final ImageView iv; private final TextView tv;
private finalTextView tv2; public MyViewHolder(View itemView) { super(itemView);
iv= itemView.findViewById(R.id.iv); tv = itemView.findViewById(R.id.tv); tv2 =
itemView.findViewById(R.id.tv2); } } }
bean文件夹下Bean
import java.util.List; public class Bean { private List<JsonBean> json; private
List<LikeBean>like; public List<JsonBean> getJson() { return json; } public void
setJson(List<JsonBean> json) {this.json = json; } public List<LikeBean>
getLike() {return like; } public void setLike(List<LikeBean> like) { this.like
= like; }public static class JsonBean { private int id; private String image;
privateString name; private String price; private String agio; private String
amount; private String chart; private List<SwiperBean> swiper; public int
getId() {return id; } public void setId(int id) { this.id = id; } public String
getImage() {return image; } public void setImage(String image) { this.image =
image; }public String getName() { return name; } public void setName(String
name) {this.name = name; } public String getPrice() { return price; } public
voidsetPrice(String price) { this.price = price; } public String getAgio() {
returnagio; } public void setAgio(String agio) { this.agio = agio; } public
String getAmount() {return amount; } public void setAmount(String amount) { this
.amount = amount; } public String getChart() { return chart; } public void
setChart(String chart) {this.chart = chart; } public List<SwiperBean>
getSwiper() {return swiper; } public void setSwiper(List<SwiperBean> swiper) {
this.swiper = swiper; } public static class SwiperBean { private String img;
publicString getImg() { return img; } public void setImg(String img) { this.img
= img; } } }public static class LikeBean { private int id; private String image;
privateString name; private String price; private String amount; private String
chart; private List<SwiperBeanX> swiper; public int getId() { return id; }
public voidsetId(int id) { this.id = id; } public String getImage() { return
image; } public void setImage(String image) { this.image = image; } public
String getName() {return name; } public void setName(String name) { this.name =
name; }public String getPrice() { return price; } public void setPrice(String
price) {this.price = price; } public String getAmount() { return amount; }
public voidsetAmount(String amount) { this.amount = amount; } public String
getChart() {return chart; } public void setChart(String chart) { this.chart =
chart; }public List<SwiperBeanX> getSwiper() { return swiper; } public void
setSwiper(List<SwiperBeanX> swiper) {this.swiper = swiper; } public static class
SwiperBeanX { private String img; public String getImg() { return img; } public
voidsetImg(String img) { this.img = img; } } } }
热门工具 换一换