Android:下拉刷新+加載更多+滑動(dòng)刪除實(shí)例講解
小伙伴們?cè)诠涮詫毣蛘呤歉鞣Napp上,都可以看到這樣的功能,下拉刷新和加載更多以及滑動(dòng)刪除,刷新,指刷洗之后使之變新,比喻突破舊的而創(chuàng)造出新的,比如在手機(jī)上瀏覽新聞的時(shí)候,使用下拉刷新的功能,我們可以第一時(shí)間掌握最新消息,加載更多是什么nie,簡(jiǎn)單來(lái)說(shuō)就是在網(wǎng)頁(yè)上逛淘寶的時(shí)候,我們可以點(diǎn)擊下一頁(yè)來(lái)滿足我們更多的需求,但是在手機(jī)端就不一樣了,沒(méi)有上下頁(yè),怎么辦nie,方法總比困難多,細(xì)心的小伙伴可能會(huì)發(fā)現(xiàn),在手機(jī)端中,有加載更多來(lái)滿足我們的要求,其實(shí)加載更多也是分頁(yè)的一種體現(xiàn)。小伙伴在使用手機(jī)版QQ的時(shí)候,消息界面會(huì)有很多的信息,如果你也像小編一樣有強(qiáng)迫癥,想把看完的消息進(jìn)行刪除,怎么辦nie,QQ實(shí)現(xiàn)了向右滑動(dòng)刪除的功能,最近在小編的項(xiàng)目中,就遇到這樣的功能下拉刷新和加載更多以及滑動(dòng)刪除,經(jīng)過(guò)幾天的搗鼓,當(dāng)冬日的第二縷陽(yáng)光照到小編身上的時(shí)候,小編終于把她搗鼓出來(lái)了,今天這篇博客,小編主要和小伙伴們分享下拉刷新和加載更多一級(jí)滑動(dòng)刪除,希望可以幫助到有需要的小伙伴,還請(qǐng)小伙伴們多多指教。
首先我們需要來(lái)編寫xml里面的內(nèi)容,我們命名為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>
接著,我們來(lái)編寫list_item里面有關(guān)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>
第三步,我們來(lái)編寫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>
第四步,我們來(lái)編寫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>
第五步,我們來(lái)編寫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>
至此,關(guān)于我們的布局xml文件的代碼都已經(jīng)寫完了,接著,我們開始編寫java類里面的代碼,首先,我們來(lái)編寫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;
}
}
接著,我們來(lái)編寫適配器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;
}
}
最后我們來(lái)編寫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 實(shí)現(xiàn) 下拉刷新和加載以及滑動(dòng)刪除的知識(shí)整理,謝謝大家對(duì)本站的支持!
- Android Handler 機(jī)制實(shí)現(xiàn)原理分析
- Android 個(gè)人理財(cái)工具六:顯示賬單明細(xì) 下
- Android 個(gè)人理財(cái)工具五:顯示賬單明細(xì) 上
- Android 個(gè)人理財(cái)工具四:添加賬單頁(yè)面 下
- Android仿微信通訊錄打造帶懸停頭部的分組列表(上)
- 最常見的猜拳小游戲Android代碼實(shí)現(xiàn)
- Android 音樂(lè)播放器的開發(fā)實(shí)例詳解
- Android開發(fā)之ListView、GridView 詳解及示例代碼
- Android 應(yīng)用中插入廣告的實(shí)例
- Android系統(tǒng)進(jìn)程間通信Binder機(jī)制在應(yīng)用程序框架層的Java接口源代碼分析
- Android WebP 圖片壓縮與傳輸
相關(guān)文章
Flutter使用texture_rgba_renderer實(shí)現(xiàn)桌面端渲染視頻詳解
這篇文章主要為大家介紹了Flutter如何使用texture_rgba_renderer實(shí)現(xiàn)桌面端渲染視頻,文中的示例代碼講解詳細(xì),需要的可以了解一下2023-07-07
Android獲取手機(jī)安裝應(yīng)用程序的詳細(xì)信息
在開發(fā)Android應(yīng)用時(shí),有時(shí)我們需要獲取設(shè)備上已安裝的所有應(yīng)用程序的信息,這篇文章為大家整理了一些常用的方法,希望對(duì)大家有所幫助2025-03-03
Android 6.0 藍(lán)牙搜索不到設(shè)備原因,MIUI權(quán)限申請(qǐng)機(jī)制方法
今天小編就為大家分享一篇Android6.0 藍(lán)牙搜索不到設(shè)備原因,MIUI權(quán)限申請(qǐng)機(jī)制方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android原生側(cè)滑控件DrawerLayout使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android原生側(cè)滑控件DrawerLayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android UI實(shí)時(shí)預(yù)覽和編寫的各種技巧
大家好,今天給大家分享的是Android中實(shí)時(shí)預(yù)覽UI和編寫UI的各種技巧,2015-11-11
Rxjava+Retrofit+Okhttp進(jìn)行網(wǎng)絡(luò)訪問(wèn)及數(shù)據(jù)解析
這篇文章主要介紹了Rxjava+Retrofit+Okhttp進(jìn)行網(wǎng)絡(luò)訪問(wèn)及數(shù)據(jù)解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
Android關(guān)鍵字persistent詳細(xì)分析
這篇文章主要介紹了Android關(guān)鍵字persistent的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Android自定義View實(shí)現(xiàn)投票進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)投票進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android使用SoundPool實(shí)現(xiàn)播放音頻
這篇文章主要為大家詳細(xì)介紹了Android使用SoundPool實(shí)現(xiàn)播放音頻,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05

