侧滑删除
侧滑删除就是使用了第三方的控件SwipeLayout,具体使用方式就是一个布局文件,类似于抽屉的创建方式,可以实现侧滑删除的功能。
代码
<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/itemLayout_id">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:id="@+id/remove_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#f00"
android:textSize="48sp"
android:textStyle="bold"
android:textColor="#fff"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/pic_item"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_margin="20dp"/>
<TextView
android:id="@+id/title_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:text="title"
android:layout_toRightOf="@id/pic_item"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/foods_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="title"
android:layout_toRightOf="@id/pic_item"
android:layout_below="@id/title_item"
android:layout_margin="10dp"/>
</RelativeLayout>
</com.daimajia.swipe.SwipeLayout>
package com.example.hengxiang_01;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listItemId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItemId = (ListView) findViewById(R.id.list_item_id);
new MyAsy().equals("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1");
}
class MyAsy extends AsyncTask<String,Void, List<Bean.DataBean>>{
@Override
protected List<Bean.DataBean> doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() == 200){
InputStream inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String str = "";
while ((str = br.readLine()) != null){
sb.append(str);
}
Bean bean = new Gson().fromJson(sb.toString(), Bean.class);
List<Bean.DataBean> data = bean.getData();
return data;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<Bean.DataBean> dataBeans) {
super.onPostExecute(dataBeans);
ListBaseAdap listBaseAdap = new ListBaseAdap(dataBeans, MainActivity.this);
listItemId.setAdapter(listBaseAdap);
}
}
}
package com.example.hengxiang_01;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import java.util.ArrayList;
import java.util.List;
public class ListBaseAdap extends BaseSwipeAdapter {
private List<Bean.DataBean> lists;
private Context context;
private LayoutInflater layoutInflater;
private RequestOptions requestOptions;
private List<View> views = new ArrayList<>();
public ListBaseAdap(List<Bean.DataBean> lists, Context context) {
this.lists = lists;
this.context = context;
layoutInflater = LayoutInflater.from(context);
requestOptions = new RequestOptions();
requestOptions.circleCrop();
}
@Override
public int getSwipeLayoutResourceId(int position) {
return position;
}
@Override
public View generateView(int position, ViewGroup parent) {
return views.get(position);
}
@Override
public void fillValues(int position, View convertView) {
MyHolder myHolder;
if (convertView == null){
myHolder = new MyHolder();
convertView = layoutInflater.inflate(R.layout.itemlayout,null);
myHolder.itemLayoutId = (SwipeLayout) convertView.findViewById(R.id.itemLayout_id);
myHolder.removeItem = (Button) convertView.findViewById(R.id.remove_item);
myHolder.picItem = (ImageView) convertView.findViewById(R.id.pic_item);
myHolder.titleItem = (TextView) convertView.findViewById(R.id.title_item);
myHolder.foodsItem = (TextView) convertView.findViewById(R.id.foods_item);
convertView.setTag(myHolder);
}else {
myHolder = (MyHolder) convertView.getTag();
}
myHolder.titleItem.setText(lists.get(position).getTitle());
myHolder.foodsItem.setText(lists.get(position).getFood_str());
Glide.with(context).load(lists.get(position).getPic()).apply(requestOptions).into(myHolder.picItem);
myHolder.itemLayoutId.setShowMode(SwipeLayout.ShowMode.LayDown);
myHolder.removeItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
views.add(convertView);
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class MyHolder{
private SwipeLayout itemLayoutId;
private Button removeItem;
private ImageView picItem;
private TextView titleItem;
private TextView foodsItem;
}
viewpager多图显示
package com.example.viewpager_01;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private LinearLayout layoutId;
private ViewPager viewPagerId;
private ArrayList<ImageView> imageViewArrayList = new ArrayList<>();
private int[] arr = new int[]{R.mipmap.d1,R.mipmap.d2,R.mipmap.d3,R.mipmap.d4,R.mipmap.d5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
layoutId = (LinearLayout) findViewById(R.id.layout_id);
viewPagerId = (ViewPager) findViewById(R.id.viewPager_id);
for (int i = 0; i < arr.length; i++) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(arr[i]);
imageViewArrayList.add(imageView);
}
viewPagerId.setPageMargin(10);
viewPagerId.setOffscreenPageLimit(3);
layoutId.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return viewPagerId.dispatchTouchEvent(event);
}
});
viewPagerId.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return imageViewArrayList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(imageViewArrayList.get(position));
return imageViewArrayList.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(imageViewArrayList.get(position));
}
});
}
}