Created
July 25, 2015 18:31
-
-
Save Kun-Yao-Lin/0c57cfda3b428e79e12f to your computer and use it in GitHub Desktop.
CustomAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="tw.com.arnold_lin.customlistview" > | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.com.arnold_lin.customlistview; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.os.AsyncTask; | |
import android.util.AttributeSet; | |
import android.util.DisplayMetrics; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.FrameLayout; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by arnold_lin on 15/7/25. | |
*/ | |
public class CustomAdapter extends BaseAdapter { | |
public static class CustomView extends LinearLayout { | |
private String originalText = ""; | |
private String changedText = ""; | |
private int originalColor = 0; | |
private int changedColor = 0; | |
private ImageView imageView ; | |
private FrameLayout bottomBar ; | |
private TextView title ; | |
public CustomView(Context context) { | |
super(context); | |
initialize(context); | |
} | |
public CustomView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
initialize(context); | |
} | |
@TargetApi(21) | |
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
initialize(context); | |
} | |
@TargetApi(21) | |
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
initialize(context); | |
} | |
private void initialize(Context context){ | |
DisplayMetrics metrics = getResources().getDisplayMetrics(); | |
setOrientation(VERTICAL); | |
imageView = new ImageView(context); | |
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); | |
imageView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, metrics.widthPixels)); | |
addView(imageView); | |
bottomBar = new FrameLayout(context); | |
bottomBar.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); | |
addView(bottomBar); | |
title = new TextView(context); | |
title.setGravity(Gravity.CENTER); | |
title.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER)); | |
bottomBar.addView(title); | |
} | |
public void setOriginalText(String text){ | |
originalText = text; | |
} | |
public void setOriginalColor(int originalColor) { | |
this.originalColor = originalColor; | |
} | |
public void setChangedText(String changedText) { | |
this.changedText = changedText; | |
} | |
public void setChangedColor(int changedColor) { | |
this.changedColor = changedColor; | |
} | |
public FrameLayout getBottomBar() { | |
return bottomBar; | |
} | |
public ImageView getImageView() { | |
return imageView; | |
} | |
public TextView getTitle() { | |
return title; | |
} | |
public int getChangedColor() { | |
return changedColor; | |
} | |
public String getChangedText() { | |
return changedText; | |
} | |
public int getOriginalColor() { | |
return originalColor; | |
} | |
public String getOriginalText() { | |
return originalText; | |
} | |
} | |
public static interface CustomDataBinder{ | |
public void loadImage(ImageView imageView); | |
public String getOriginalText(); | |
public String getChangedText(); | |
public int getOriginalColor(); | |
public int getChangedColor(); | |
} | |
public class ViewHolder { | |
CustomView view; | |
} | |
private final List<CustomDataBinder> binders = new ArrayList<>(); | |
private final Context context; | |
private AsyncTask<Void, Void, Void> asyncTask = null; | |
private final List<CustomAdapter.CustomView> customViews = new ArrayList<>(); | |
public CustomAdapter(Context context) { | |
this.context = context; | |
} | |
@Override | |
public int getCount() { | |
return binders.size(); | |
} | |
@Override | |
public Object getItem(int i) { | |
return binders.get(i); | |
} | |
@Override | |
public long getItemId(int i) { | |
return 0; | |
} | |
@Override | |
public View getView(int i, View view, ViewGroup viewGroup) { | |
ViewHolder viewHolder = null; | |
if(view == null){ | |
CustomView customView = new CustomView(context); | |
view = customView; | |
viewHolder = new ViewHolder(); | |
viewHolder.view = customView; | |
view.setTag(viewHolder); | |
}else { | |
viewHolder = (ViewHolder) view.getTag(); | |
} | |
CustomDataBinder binder = binders.get(i); | |
CustomView customView = viewHolder.view; | |
binder.loadImage(customView.getImageView()); | |
customView.setOriginalColor(binder.getOriginalColor()); | |
customView.getBottomBar().setBackgroundColor(binder.getOriginalColor()); | |
customView.setChangedColor(binder.getChangedColor()); | |
customView.setOriginalText(binder.getOriginalText()); | |
customView.getTitle().setText(binder.getOriginalText()); | |
customView.setChangedText(binder.getChangedText()); | |
register(customView); | |
return view; | |
} | |
public void setBinders(List<CustomDataBinder> binders){ | |
if(binders == null){ | |
return; | |
} | |
this.binders.clear(); | |
this.binders.addAll(binders); | |
notifyDataSetChanged(); | |
} | |
public void recover(){ | |
for(CustomAdapter.CustomView customView : customViews){ | |
customView.getTitle().setText(customView.getOriginalText()); | |
customView.getBottomBar().setBackgroundColor(customView.getOriginalColor()); | |
} | |
} | |
public void register(CustomAdapter.CustomView customView){ | |
if(customView != null && customViews.contains(customView) == false){ | |
customViews.add(customView); | |
} | |
} | |
public void unregister(CustomAdapter.CustomView customView){ | |
if(customView != null && customViews.contains(customView) == true){ | |
customViews.remove(customView); | |
} | |
} | |
public void start(final int countTime) { | |
if (asyncTask == null) { | |
asyncTask = new AsyncTask<Void, Void, Void>() { | |
@Override | |
protected Void doInBackground(Void... voids) { | |
try { | |
Thread.sleep(countTime * 1000); | |
} catch (InterruptedException e) { | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void aVoid) { | |
super.onPostExecute(aVoid); | |
for(CustomAdapter.CustomView customView : customViews){ | |
customView.getTitle().setText(customView.getChangedText()); | |
customView.getBottomBar().setBackgroundColor(customView.getChangedColor()); | |
} | |
asyncTask = null; | |
} | |
@Override | |
protected void onCancelled() { | |
super.onCancelled(); | |
asyncTask = null; | |
} | |
}; | |
asyncTask.execute(); | |
} else { | |
asyncTask.cancel(true); | |
asyncTask = null; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.com.arnold_lin.customlistview; | |
import android.app.Activity; | |
import android.graphics.Bitmap; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.ViewGroup; | |
import android.widget.AbsListView; | |
import android.widget.FrameLayout; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import com.nostra13.universalimageloader.core.DisplayImageOptions; | |
import com.nostra13.universalimageloader.core.ImageLoader; | |
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; | |
import com.nostra13.universalimageloader.core.assist.ImageScaleType; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends Activity implements AbsListView.OnScrollListener{ | |
private ImageLoader imageLoader = ImageLoader.getInstance(); | |
private ListView listView ; | |
private CustomAdapter adapter; | |
public static final DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions.Builder() | |
// .cacheInMemory(true) | |
// .considerExifParams(true) | |
.showImageOnFail(R.mipmap.ic_launcher) | |
// .showImageOnFail(R.drawable.nicemdm_logo_) | |
.cacheOnDisk(true) | |
.cacheInMemory(true) | |
.showImageForEmptyUri(R.mipmap.ic_launcher) | |
// .showImageForEmptyUri(R.drawable.nicemdm_logo_) | |
.imageScaleType(ImageScaleType.EXACTLY) | |
.bitmapConfig(Bitmap.Config.ARGB_8888); | |
public static final DisplayImageOptions displayImageOptions = displayImageOptionsBuilder.build(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext()).build(); | |
if(!imageLoader.isInited()){ | |
imageLoader.init(configuration); | |
} | |
listView = new ListView(this); | |
listView.setOnScrollListener(this); | |
listView.setDividerHeight(20); | |
listView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); | |
setContentView(listView); | |
adapter = new CustomAdapter(this); | |
listView.setAdapter(adapter); | |
getTestData(); | |
start(); | |
} | |
private void getTestData(){ | |
List<CustomAdapter.CustomDataBinder> binders = new ArrayList<>(); | |
final int o_color = getResources().getColor(android.R.color.holo_green_dark); | |
final int c_color = getResources().getColor(android.R.color.holo_red_dark); | |
for(int i = 0 ; i < 10 ; i++){ | |
final int count = i; | |
CustomAdapter.CustomDataBinder binder = new CustomAdapter.CustomDataBinder() { | |
@Override | |
public void loadImage(ImageView imageView) { | |
imageLoader.displayImage("http://get.aca.ntu.edu.tw/getcdb/retrieve/331220/099_0089_251.png", imageView, displayImageOptions); | |
} | |
@Override | |
public String getOriginalText() { | |
return "OriginalText " + count; | |
} | |
@Override | |
public String getChangedText() { | |
return "ChangedText " + count; | |
} | |
@Override | |
public int getOriginalColor() { | |
return o_color; | |
} | |
@Override | |
public int getChangedColor() { | |
return c_color; | |
} | |
}; | |
binders.add(binder); | |
} | |
adapter.setBinders(binders); | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
Log.d("AbsListView Listener", "firstVisibleItem : " + firstVisibleItem); | |
int position = firstVisibleItem; | |
Log.d("AbsListView Listener",""+position); | |
// TimeController.getInstance().start(1); | |
// boolean loadMore = /* maybe add a padding */ | |
// firstVisibleItem + visibleItemCount >= totalItemCount; | |
// | |
// Log.d("AbsListView Listener",""+loadMore); | |
// | |
// if(loadMore) { | |
// adapter_style.addDynamicCount(visibleItemCount); | |
// } | |
} | |
@Override | |
public void onScrollStateChanged(AbsListView absListView, int i) { | |
Log.d("onScrollStateChanged",""+i); | |
switch (i){ | |
case 0: | |
start(); | |
break; | |
case 1: | |
adapter.recover(); | |
break; | |
case 2: | |
break; | |
} | |
} | |
private void start(){ | |
adapter.start(5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment