首页 > 代码库 > android listview反序和正序显示
android listview反序和正序显示
本人很喜欢晚上睡觉用暴风语音看电影,如图:
现在写个demo 演示下
MainActivity.java
package com.example.listview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {
private Button btn;
private ListView listview;
private List<String> datas;
private MyAdapter adapter;
private boolean isFu;
private List<String> mDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
initData();
listview.setOnItemClickListener(this);
}
private void initData() {
datas = new ArrayList<String>();
mDatas = new ArrayList<String>();
for(int i=0;i<100;i++){
datas.add("测试"+i);
}
for(int i=datas.size()-1;i>=0;i--){
mDatas.add(datas.get(i));
}
adapter = new MyAdapter();
adapter.setDatas(datas);
listview.setAdapter(adapter);
}
class MyAdapter extends BaseAdapter{
private List<String> datas;
public void setDatas(List<String> datas) {
this.datas = datas;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setText(datas.get(position));
tv.setTextSize(20);
return tv;
}
}
@Override
public void onClick(View v) {
if(isFu){
btn.setText("正序");
isFu = false;
adapter.setDatas(datas);
adapter.notifyDataSetChanged();
}else{
btn.setText("反序");
isFu = true;
adapter.setDatas(mDatas);
adapter.notifyDataSetChanged();
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String content = (String) adapter.getItem(position);
if(isFu){
Toast.makeText(getApplicationContext(), content, 0).show();
}else{
Toast.makeText(getApplicationContext(), content, 0).show();
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="正序" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:layout_below="@id/btn"
android:layout_marginTop="3dp"
></ListView>
</RelativeLayout>
android listview反序和正序显示