标签:color return res class 一个 ota bool instance 平滑
viewholder_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 8 9 <ImageView 10 android:id="@+id/iv" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" /> 13 14 <TextView 15 android:id="@+id/tv" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" /> 18 </LinearLayout>
ViewHolderAdapter.java
1 package sowell.oracle.com.listviewholder; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.ImageView; 9 import android.widget.TextView; 10 import android.widget.Toast; 11 12 import java.util.List; 13 14 /** 15 * Created by 94965 on 2017/11/11. 16 */ 17 18 public class ViewHolderAdapter extends BaseAdapter { 19 20 private List<String> mData; //储存数据 21 private LayoutInflater mInflater; //实例化布局 22 public Context context; 23 24 public ViewHolderAdapter(Context context,List<String> data){ 25 super(); 26 this.context=context; 27 this.mData=data; 28 mInflater=LayoutInflater.from(context); 29 } 30 31 32 33 @Override 34 public int getCount() { 35 return mData.size(); 36 } 37 38 @Override 39 public Object getItem(int position) { 40 return mData.get(position); 41 } 42 43 @Override 44 public long getItemId(int position) { 45 return position; 46 } 47 48 @Override 49 public View getView(final int position, View convertView, final ViewGroup parent) { 50 ViewHolder holder=null; 51 //判断是否缓存 52 if(convertView==null){ 53 holder=new ViewHolder(); 54 //通过LayoutInflater实例化布局 55 convertView=mInflater.inflate(R.layout.viewholder_item,null); 56 holder.img=(ImageView)convertView.findViewById(R.id.iv); 57 holder.title=(TextView)convertView.findViewById(R.id.tv); 58 holder.img.setOnClickListener(new View.OnClickListener() { 59 @Override 60 public void onClick(View v) { 61 //对item中的imageview设置点击事件 62 Toast.makeText(context,"第"+(position+1)+"张图片被点击",Toast.LENGTH_SHORT).show(); 63 } 64 }); 65 convertView.setTag(holder); 66 }else { 67 //通过tag找到缓存‘的布局 68 holder=(ViewHolder)convertView.getTag(); 69 } 70 71 //设置布局中控件要显示的视图 72 holder.img.setBackgroundResource(R.mipmap.ic_launcher); 73 holder.title.setText(mData.get(position)); 74 return convertView; 75 } 76 77 78 79 public final class ViewHolder{ 80 public ImageView img; 81 public TextView title; 82 } 83 }
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="wrap_content" 6 android:orientation="vertical" 7 tools:context="sowell.oracle.com.listviewholder.MainActivity"> 8 9 10 11 12 <ListView 13 android:id="@+id/lv" 14 android:layout_width="match_parent" 15 android:layout_height="450dp" 16 android:divider="@android:color/darker_gray" 17 android:dividerHeight="1dp" 18 > 19 </ListView> 20 21 22 23 24 <Button 25 android:id="@+id/btn" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:text="add" /> 29 30 31 <LinearLayout 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:orientation="horizontal" 35 > 36 <EditText 37 android:id="@+id/et" 38 android:layout_weight="1" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" /> 41 <Button 42 android:id="@+id/btn1" 43 android:layout_weight="1" 44 android:onClick="moveto" 45 android:text="移动" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" /> 48 </LinearLayout> 49 50 51 </LinearLayout>
MainActivity.java
1 package sowell.oracle.com.listviewholder; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.MotionEvent; 7 import android.view.View; 8 import android.view.WindowManager; 9 import android.widget.AbsListView; 10 import android.widget.AdapterView; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.widget.ListView; 14 import android.widget.Toast; 15 16 import java.util.ArrayList; 17 import java.util.List; 18 19 public class MainActivity extends AppCompatActivity { 20 21 public ListView listView; 22 public ViewHolderAdapter adapter; 23 public List<String> list; 24 public String string; 25 public Button bt; 26 public Button move; 27 private EditText et; 28 public void init(){ 29 30 move=(Button)findViewById(R.id.btn1); 31 et=(EditText)findViewById(R.id.et); 32 bt=(Button)findViewById(R.id.btn); 33 bt.setOnClickListener(new View.OnClickListener() { 34 @Override 35 public void onClick(View v) { 36 add(); 37 } 38 }); 39 list=new ArrayList<String>(); 40 for(int i=0;i<=20;i++) 41 { 42 string=""+i; 43 list.add(string); 44 } 45 adapter=new ViewHolderAdapter(this,list); 46 listView=(ListView)findViewById(R.id.lv); 47 listView.setAdapter(adapter); 48 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 49 @Override 50 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 51 //对整个item设置点击事件 52 String s=list.get(position); 53 Toast.makeText(MainActivity.this,"第"+(position+1)+"个item被点击",Toast.LENGTH_SHORT).show(); 54 } 55 }); 56 listView.setOnTouchListener(new View.OnTouchListener() { 57 @Override 58 public boolean onTouch(View v, MotionEvent event) { 59 switch (event.getAction()) { 60 case MotionEvent.ACTION_DOWN: 61 Toast.makeText(MainActivity.this, "ACTION_DOWN", Toast.LENGTH_SHORT).show(); 62 break; 63 64 case MotionEvent.ACTION_MOVE: 65 Toast.makeText(MainActivity.this, "ACTION_MOVE", Toast.LENGTH_SHORT).show(); 66 break; 67 68 case MotionEvent.ACTION_UP: 69 Toast.makeText(MainActivity.this, "ACTION_UP", Toast.LENGTH_SHORT).show(); 70 break; 71 } 72 return false; 73 } 74 }); 75 76 listView.setOnScrollListener(new AbsListView.OnScrollListener() { 77 @Override 78 public void onScrollStateChanged(AbsListView view, int scrollState) { 79 switch (scrollState){ 80 case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: 81 Toast.makeText(MainActivity.this,"滑动停止",Toast.LENGTH_SHORT).show(); 82 break; 83 84 case SCROLL_STATE_TOUCH_SCROLL: 85 Toast.makeText(MainActivity.this,"正在滚动",Toast.LENGTH_SHORT).show(); 86 break; 87 88 case SCROLL_STATE_FLING: 89 Toast.makeText(MainActivity.this,"惯性继续滚动",Toast.LENGTH_SHORT).show(); 90 break; 91 } 92 } 93 94 @Override 95 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 96 Toast.makeText(MainActivity.this,""+firstVisibleItem,Toast.LENGTH_SHORT).show(); 97 } 98 }); 99 } 100 101 102 @Override 103 protected void onCreate(Bundle savedInstanceState) { 104 super.onCreate(savedInstanceState); 105 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); //防止被输入框被遮盖 106 setContentView(R.layout.activity_main); 107 init(); 108 } 109 110 public void moveto(View view){ 111 int position=Integer.parseInt(et.getText().toString()); 112 if(position>=0&&position<=list.size()-1) 113 listView.smoothScrollByOffset(position);//平滑移动到布局第一个元素为position 114 //listView.smoothScrollToPosition(position); 115 } 116 117 public void add(){ 118 list.add("new"); 119 adapter.notifyDataSetChanged(); //动态添加listview中的数据 120 //listView.setSelection(list.size()-1);//瞬间移动 121 listView.smoothScrollToPosition(list.size()-1);//平滑移动到布局中有该位置 122 123 } 124 125 126 127 }
标签:color return res class 一个 ota bool instance 平滑
原文地址:http://www.cnblogs.com/zhengzhe/p/7821379.html