Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
通用Adapter与ListView滚动时不加载图片的封装,摘自http://www.kymjs.com/code/2015/04/28/01/
/*
* Copyright (c) 2014, 张涛.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kymjs.kjframe.widget;
/**
* 万能Adapter,查看KJAdapter
*
* @author kymjs(http://www.kymjs.com/)
*/
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.kymjs.kjframe.KJBitmap;
public class AdapterHolder {
private final SparseArray<View> mViews;
private final int mPosition;
private final View mConvertView;
private AdapterHolder(ViewGroup parent, int layoutId, int position) {
this.mPosition = position;
this.mViews = new SparseArray<View>();
mConvertView = LayoutInflater.from(parent.getContext()).inflate(
layoutId, parent, false);
// setTag
mConvertView.setTag(this);
}
/**
* 拿到全部View
*
* @return
*/
public SparseArray<View> getAllView() {
return mViews;
}
/**
* 拿到一个ViewHolder对象
*
* @param convertView
* @param parent
* @param layoutId
* @param position
* @return
*/
public static AdapterHolder get(View convertView, ViewGroup parent,
int layoutId, int position) {
if (convertView == null) {
return new AdapterHolder(parent, layoutId, position);
} else {
return (AdapterHolder) convertView.getTag();
}
}
public View getConvertView() {
return mConvertView;
}
/**
* 通过控件的Id获取对于的控件,如果没有则加入views
*
* @param viewId
* @return
*/
@SuppressWarnings("unchecked")
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
/**
* 为TextView设置字符串
*
* @param viewId
* @param text
* @return
*/
public AdapterHolder setText(int viewId, CharSequence text) {
TextView view = getView(viewId);
view.setText(text);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param drawableId
* @return
*/
public AdapterHolder setImageResource(int viewId, int drawableId) {
ImageView view = getView(viewId);
view.setImageResource(drawableId);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param bm
* @return
*/
public AdapterHolder setImageBitmap(int viewId, Bitmap bm) {
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param url
* @return
*/
public AdapterHolder setImageByUrl(KJBitmap bitmap, int viewId, String url) {
bitmap.display(getView(viewId), url);
return this;
}
public int getPosition() {
return mPosition;
}
}
/*
* Copyright (c) 2014, 张涛.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kymjs.kjframe.widget;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* 对ViewHolder的封装,以及更方便的控制ListView滑动过程中不加载图片
*
* @param <T>
* @author kymjs (https://www.kymjs.com/)
*/
public abstract class KJAdapter<T> extends BaseAdapter implements
AbsListView.OnScrollListener {
protected Collection<T> mDatas;
protected final int mItemLayoutId;
protected AbsListView mList;
protected boolean isScrolling;
protected Context mCxt;
protected LayoutInflater mInflater;
private AbsListView.OnScrollListener listener;
public KJAdapter(AbsListView view, Collection<T> mDatas, int itemLayoutId) {
if (mDatas == null) {
mDatas = new ArrayList<T>(0);
}
this.mDatas = mDatas;
this.mItemLayoutId = itemLayoutId;
this.mList = view;
mCxt = view.getContext();
mInflater = LayoutInflater.from(mCxt);
mList.setOnScrollListener(this);
}
public void refresh(Collection<T> datas) {
if (datas == null) {
datas = new ArrayList<T>(0);
}
this.mDatas = datas;
notifyDataSetChanged();
}
public void addOnScrollListener(AbsListView.OnScrollListener l) {
this.listener = l;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public T getItem(int position) {
if (mDatas instanceof List) {
return ((List<T>) mDatas).get(position);
} else if (mDatas instanceof Set) {
return new ArrayList<T>(mDatas).get(position);
} else {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final AdapterHolder viewHolder = getViewHolder(position, convertView,
parent);
convert(viewHolder, getItem(position), isScrolling, position);
return viewHolder.getConvertView();
}
private AdapterHolder getViewHolder(int position, View convertView,
ViewGroup parent) {
return AdapterHolder.get(convertView, parent, mItemLayoutId, position);
}
public void convert(AdapterHolder helper, T item,
boolean isScrolling) {
}
public void convert(AdapterHolder helper, T item, boolean isScrolling,
int position) {
convert(helper, getItem(position), isScrolling);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 设置是否滚动的状态
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
isScrolling = false;
this.notifyDataSetChanged();
} else {
isScrolling = true;
}
if (listener != null) {
listener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (listener != null) {
listener.onScroll(view, firstVisibleItem, visibleItemCount,
totalItemCount);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.