具有弹性的ListView+自定义ListViewBar+背景特效

按之前一样.直接贴代码这就是道理,下编我把自定义具有弹性的ListView 与自定义具有弹性的Scroll结合后冲突的问题的解决方案,还有异步加载对其产生不良影响的解决方案,请关注:


这篇的全部代码如下:

package com.jsd.demo.listview;


import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.jsd.demo.R;


/**
 * 
 * @author jiangshide
 *
 */
public class MyListSlide extends Activity{

CustomerListView myList;
ArrayList<Bean> myListArr;
MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_list);
findViewById();
myList.setSelector(R.drawable.ly_background_focus);
try {
Field field = AbsListView.class.getDeclaredField("mFastScroller");
field.setAccessible(true);
Object obj = field.get(myList);
field = field.getType().getDeclaredField("mThumbDrawable");
field.setAccessible(true);
Drawable drawable = (Drawable)field.get(obj);
drawable = getResources().getDrawable(R.drawable.abouts);
field.set(obj,drawable);
Toast.makeText(this, field.getType().getName(), 1000).show();
} catch (Exception e) {
e.printStackTrace();
}
}

private void findViewById(){
myList = (CustomerListView) findViewById(R.id.myListJsd);
myListArr = new ArrayList<Bean>();
loadData();
adapter = new MyAdapter(getApplicationContext(), myListArr);
myList.setAdapter(adapter);
}

public void loadData(){
Bean b = new Bean();
for (int i = 0; i < 100; i++) {
b.setTitle("this is for title!");
b.setContent("this is for content!");
myListArr.add(b);
}
}

class MyAdapter extends BaseAdapter{


private Context mContext;
private ArrayList<Bean> arrs;

public MyAdapter(Context c,ArrayList<Bean> al) {
this.mContext = c;
this.arrs = al;
}

@Override
public int getCount() {
return arrs.size();
}


@Override
public Object getItem(int position) {
return arrs.get(position);
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(arrs == null){
return null;
}
if(convertView == null){
MyView view = new MyView(mContext);
view.updateView(arrs.get(position));
convertView = view;
}else{
((MyView)convertView).updateView(arrs.get(position));
}
return convertView;
}

}

class MyView extends LinearLayout{


private Context mContext;

private TextView ItemTitle;
private TextView ItemText;

public MyView(Context c) {
super(c);
this.mContext = c;
initializze(c);
}

public void initializze(Context c){
this.mContext = c;
View view = LayoutInflater.from(mContext).inflate(R.layout.my_list_item, null);
ItemTitle = (TextView) view.findViewById(R.id.ItemTitle);
ItemText = (TextView) view.findViewById(R.id.ItemText);
addView(view);
}

public void updateView(Bean b){
ItemTitle.setText(b.getTitle());
ItemText.setText(b.getContent());
}
}

class Bean{
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}

以上为主要的ACTIVITY为自定义的

package com.jsd.demo.listview;


import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.TranslateAnimation;
import android.widget.ListView;


/**
 * 
 * @author jiangshide
 *
 */
public class CustomerListView extends ListView{


private Context mContext;
private boolean outBound = false;
private int distance;
private int firstOut;

public CustomerListView(Context c) {
super(c);
this.mContext = c;
}

public CustomerListView(Context c,AttributeSet attrs) {
super(c,attrs);
this.mContext = c;
}

public CustomerListView(Context c,AttributeSet attrs,int defStyle) {
super(c,attrs,defStyle);
this.mContext = c;
}

GestureDetector gestureDetector = new GestureDetector(new OnGestureListener() {

@Override
public boolean onSingleTapUp(MotionEvent e) {

return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
int firstPos = getFirstVisiblePosition();
int lastPos = getLastVisiblePosition();
int itemCount = getCount();
if(outBound && firstPos != 0 && lastPos != (itemCount - 1)){
scrollTo(0, 0);
return false;
}
View firstView = getChildAt(firstPos);
if(!outBound){
firstOut = (int) e2.getRawX();
}
if(firstView != null && (outBound || (firstPos == 0 && firstView.getTop() == 0 && distanceY < 0))){
distance = (int) (firstOut - e2.getRawY());
scrollBy(0, distance/2);
return true;
}
return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {


return false;
}

@Override
public boolean onDown(MotionEvent e) {


return false;
}
});

public boolean dispatchTouchEvent(MotionEvent ev) {
int act = ev.getAction();
if((act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_CANCEL) && outBound){
outBound = false;
}
if(!gestureDetector.onTouchEvent(ev)){
outBound = false;
}else{
outBound = true;
}
Rect rect = new Rect();
getLocalVisibleRect(rect);
TranslateAnimation am = new TranslateAnimation(0,0,-rect.top,0);
am.setDuration(300);
startAnimation(am);
scrollTo(0,0);
return super.dispatchTouchEvent(ev);
};
}

以上为自定义具有弹性的ListView


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/bg"
  android:orientation="vertical"
  >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="jiangshide to demo!"
/>
  <com.jsd.demo.listview.CustomerListView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/myListJsd"
  android:fastScrollEnabled="true"
  android:focusable="true"
  android:cacheColorHint="#00000000"
  />
</LinearLayout>


<LinearLayout     
        android:layout_width="fill_parent"     
        xmlns:android="http://schemas.android.com/apk/res/android"     
        android:orientation="vertical"    
        android:layout_height="wrap_content"     
        android:id="@+id/MyListItem"     
        android:paddingBottom="3dip"     
        android:paddingLeft="10dip">    
        <TextView     
                android:layout_height="wrap_content"     
                android:layout_width="fill_parent"     
                android:id="@+id/ItemTitle"     
                android:textSize="30dip">    
        </TextView>    
        <TextView     
                android:layout_height="wrap_content"     
                android:layout_width="fill_parent"     
                android:id="@+id/ItemText">    
        </TextView>   
</LinearLayout>  


OK,以上是实现的基本代码,其实直接可以用于开发了,希望给部分朋友能带来有力的参考,我截了部分实现的效果如图:




posted @ 2011-07-08 23:54  移动应用开发  阅读(189)  评论(0编辑  收藏  举报