Android 下拉刷新+加载更多+滑动删除 Android:下拉刷新+加载更多+滑动删除实例讲解
人气:0小伙伴们在逛淘宝或者是各种app上,都可以看到这样的功能,下拉刷新和加载更多以及滑动删除,刷新,指刷洗之后使之变新,比喻突破旧的而创造出新的,比如在手机上浏览新闻的时候,使用下拉刷新的功能,我们可以第一时间掌握最新消息,加载更多是什么nie,简单来说就是在网页上逛淘宝的时候,我们可以点击下一页来满足我们更多的需求,但是在手机端就不一样了,没有上下页,怎么办nie,方法总比困难多,细心的小伙伴可能会发现,在手机端中,有加载更多来满足我们的要求,其实加载更多也是分页的一种体现。小伙伴在使用手机版QQ的时候,消息界面会有很多的信息,如果你也像小编一样有强迫症,想把看完的消息进行删除,怎么办nie,QQ实现了向右滑动删除的功能,最近在小编的项目中,就遇到这样的功能下拉刷新和加载更多以及滑动删除,经过几天的捣鼓,当冬日的第二缕阳光照到小编身上的时候,小编终于把她捣鼓出来了,今天这篇博客,小编主要和小伙伴们分享下拉刷新和加载更多一级滑动删除,希望可以帮助到有需要的小伙伴,还请小伙伴们多多指教。
首先我们需要来编写xml里面的内容,我们命名为activity_item,具体代码如下所示:
XML/HTML代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_parent" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/darker_gray" android:orientation="vertical" > <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="25sp" /> </LinearLayout>
接着,我们来编写list_item里面有关xml的代码,具体代码如下所示:
XML/HTML代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll_parent" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@android:color/darker_gray" tools:context=".MainActivity" > <TextView android:id="@+id/list_item_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textColor="#000" android:textSize="16sp" /> <FrameLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:padding="15dp" > <TextView android:id="@+id/tv_functions" android:layout_width="90dp" android:layout_height="50dp" android:layout_gravity="center" android:background="@drawable/btn_del_bg" android:gravity="center" android:textColor="@android:color/white" android:text="删除" /> <TextView android:id="@+id/tv_coating" android:layout_width="90dp" android:layout_height="50dp" android:background="@android:color/darker_gray" android:layout_gravity="center" android:visibility="gone" /> </FrameLayout> </RelativeLayout>
第三步,我们来编写main里面的xml文件,具体代码如下所示:
XML/HTML代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f0f0f0" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <me.maxwin.view.XListView android:id="@+id/xListView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" > </me.maxwin.view.XListView> </LinearLayout>
第四步,我们来编写xlistview_footer里面的xml文件,具体代码如下所示:
XML/HTML代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RelativeLayout android:id="@+id/xlistview_footer_content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" > <ProgressBar android:id="@+id/xlistview_footer_progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:visibility="invisible" /> <TextView android:id="@+id/xlistview_footer_hint_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/xlistview_footer_hint_normal" /> </RelativeLayout> </LinearLayout>
第五步,我们来编写xlistview_header里面的xml文件,具体代码如下所示:
XML/HTML代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom" > <RelativeLayout android:id="@+id/xlistview_header_content" android:layout_width="fill_parent" android:layout_height="60dp" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:orientation="vertical" android:id="@+id/xlistview_header_text"> <TextView android:id="@+id/xlistview_header_hint_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/xlistview_header_hint_normal" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/xlistview_header_last_time" android:textSize="12sp" /> <TextView android:id="@+id/xlistview_header_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" /> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/xlistview_header_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/xlistview_header_text" android:layout_centerVertical="true" android:layout_marginLeft="-35dp" android:src="@drawable/xlistview_arrow" /> <ProgressBar android:id="@+id/xlistview_header_progressbar" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignLeft="@id/xlistview_header_text" android:layout_centerVertical="true" android:layout_marginLeft="-40dp" android:visibility="invisible" /> </RelativeLayout> </LinearLayout>
至此,关于我们的布局xml文件的代码都已经写完了,接着,我们开始编写java类里面的代码,首先,我们来编写ItemActivity里面的代码,具体代码如下所示:
Java代码
import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView; public class ItemActivity extends Activity implements OnTouchListener { private float x, upx; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_item); init(); } private void init() { findViewById(R.id.ll_parent).setOnTouchListener(this); ((TextView) findViewById(R.id.tv_text)).setText(getIntent().getStringExtra("item")); } public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { x = event.getX(); } if (event.getAction() == MotionEvent.ACTION_UP) { upx = event.getX(); if (Math.abs(x - upx) > 20) { this.finish(); overridePendingTransition(0, R.anim.slide_out_to_right); } } return true; } }
接着,我们来编写适配器adapter里面的代码,具体代码如下所示:
Java代码
import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ItemAdapter extends BaseAdapter { private LayoutInflater inflater; private ArrayList<String> datas; public ItemAdapter(Context context) { inflater = LayoutInflater.from(context); } public void setData(ArrayList<String> datas) { this.datas = datas; } public int getCount() { return datas.size(); } public Object getItem(int position) { return datas.get(position); } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.list_item, null); holder.coating = (TextView) convertView.findViewById(R.id.tv_coating); holder.functions = (TextView) convertView.findViewById(R.id.tv_functions); holder.list_item_textview=(TextView) convertView.findViewById(R.id.list_item_textview); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.list_item_textview.setText(datas.get(position)); holder.coating.setVisibility(View.VISIBLE); holder.functions.setClickable(false); return convertView; } public final class ViewHolder { public TextView coating; public TextView functions; public TextView list_item_textview; } }
最后我们来编写XListViewActivity里面的代码,具体代码如下所示:
Java代码
import java.util.ArrayList; import me.maxwin.view.XListView; import me.maxwin.view.XListView.IXListViewListener; import me.maxwin.view.XListView.RemoveListener; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; public class XListViewActivity extends Activity implements IXListViewListener ,RemoveListener,OnItemClickListener{ private XListView mListView; // private ArrayAdapter<String> mAdapter; private ItemAdapter adapter; // private Context context; private ArrayList<String> items = new ArrayList<String>(); private Handler mHandler; private int start = 0; private static int refreshCnt = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); geneItems(); init(); mHandler = new Handler(); } private void init() { // TODO Auto-generated method stub mListView = (XListView) findViewById(R.id.xListView); mListView.setPullLoadEnable(true); mListView.setRemoveListener(this); mListView.setOnItemClickListener(this); // mListView.setPullLoadEnable(false); // mListView.setPullRefreshEnable(false); mListView.setXListViewListener(this); adapter=new ItemAdapter(this); adapter.setData(items); mListView.setAdapter(adapter); } private void geneItems() { for (int i = 0; i != 20; ++i) { items.add("refresh cnt " + (++start)); } } private void onLoad() { mListView.stopRefresh(); mListView.stopLoadMore(); mListView.setRefreshTime("刚刚"); } @Override public void onRefresh() { mHandler.postDelayed(new Runnable() { @Override public void run() { start = ++refreshCnt; items.clear(); geneItems(); // mAdapter.notifyDataSetChanged(); adapter=new ItemAdapter(XListViewActivity.this); adapter.setData(items); mListView.setAdapter(adapter); onLoad(); } }, 2000); } @Override public void onLoadMore() { mHandler.postDelayed(new Runnable() { @Override public void run() { geneItems(); adapter.notifyDataSetChanged(); onLoad(); } }, 2000); } @Override public void removeItem(int position) { // TODO Auto-generated method stub mListView.isSlide = false; mListView.itemView.findViewById(R.id.tv_coating).setVisibility(View.VISIBLE); items.remove(position); adapter.notifyDataSetChanged(); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Intent intent = new Intent(getApplicationContext(), ItemActivity.class); intent.putExtra("item", items.get(position)); startActivity(intent); overridePendingTransition(R.anim.slide_in_from_right, R.anim.remain_original_location); } }
至此,以上就是Android 实现 下拉刷新和加载以及滑动删除的知识整理,谢谢大家对本站的支持!
加载全部内容