Skip to content

Commit

Permalink
lsitview refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cundong committed Apr 2, 2015
1 parent 38102a1 commit 83e6802
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 26 deletions.
27 changes: 27 additions & 0 deletions res/layout/list_item_no_image.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4.0dip"
android:background="?listItemBackground"
android:minHeight="72.0dip"
android:padding="12.0dip" >

<TextView
android:id="@+id/list_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:maxLines="3"
android:minLines="2"
android:textColor="?listItemTextNorColor"
android:textIsSelectable="false"
android:textSize="@dimen/text_size_medium" />
</RelativeLayout>

</FrameLayout>
81 changes: 55 additions & 26 deletions src/com/cundong/izhihu/adapter/NewsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.cundong.izhihu.R;
import com.cundong.izhihu.entity.NewsListEntity.NewsEntity;
import com.cundong.izhihu.util.ListUtils;
import com.cundong.izhihu.util.NetWorkHelper;
import com.cundong.izhihu.util.ZhihuUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
Expand All @@ -37,9 +38,15 @@
*/
public class NewsAdapter extends MultiViewTypeBaseAdapter<NewsEntity> {

//带图item
private static final int TYPE_0 = 0;

//不带图item
private static final int TYPE_1 = 1;

//tag
private static final int TYPE_2 = 2;

private ImageLoader mImageLoader = ImageLoader.getInstance();

private ImageLoadingListener mAnimateFirstListener = new AnimateFirstDisplayListener();
Expand Down Expand Up @@ -132,38 +139,43 @@ public SparseBooleanArray getSelectedIds() {
@Override
public int getViewTypeCount() {
if (mFavoriteFalg) {
return 1;
} else {
return 2;
} else {
return 3;
}
}

@Override
public int getItemViewType(int position) {

if (mFavoriteFalg) {
return TYPE_0;
NewsEntity newsEntity = mDataList.get(position);

if (newsEntity.isTag) {
return TYPE_2;
} else {
NewsEntity newsEntity = mDataList.get(position);
if (newsEntity.isTag) {
if (NetWorkHelper.isMobile(mContext) && PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("noimage_nowifi?", false)) {
return TYPE_1;
} else {
return TYPE_0;
if (!ListUtils.isEmpty(newsEntity.images)) {
return TYPE_0;
} else {
return TYPE_1;
}
}
}
}

@Override
public int getItemResourceId(int type) {

if (mFavoriteFalg) {
switch (type) {
case TYPE_0:
return R.layout.list_item;
} else {
if (type == TYPE_0) {
return R.layout.list_item;
} else {
return R.layout.list_date_item;
}
case TYPE_1:
return R.layout.list_item_no_image;
case TYPE_2:
default:
return R.layout.list_date_item;
}
}

Expand All @@ -174,7 +186,8 @@ public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder0 = null;
ViewHolder holder1 = null;

ViewHolder holder2 = null;

switch (type) {
case TYPE_0: {

Expand All @@ -199,6 +212,17 @@ public View getView(int position, View convertView, ViewGroup parent) {

return getItemView(position, convertView, holder1, type);
}
case TYPE_2: {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(getItemResourceId(type), parent, false);
holder2 = new ViewHolder(convertView);
convertView.setTag(holder2);
} else {
holder2 = (ViewHolder) convertView.getTag();
}

return getItemView(position, convertView, holder2, type);
}
}

return null;
Expand All @@ -221,24 +245,29 @@ public View getItemView(int position, View convertView, ViewHolder holder, int t
newsTitleView.setTextColor( newsEntity.is_read ? mContext.getResources().getColor(titleReadColorId) : mContext.getResources().getColor(titleColorNorId) );
}

if (NetWorkHelper.isMobile(mContext) && PreferenceManager.getDefaultSharedPreferences(
mContext).getBoolean("noimage_nowifi?", false) ) {
newsImageView.setVisibility(View.GONE);
newsImageView.setVisibility(View.VISIBLE);
mImageLoader.displayImage(newsEntity.images.get(0), newsImageView, mOptions, mAnimateFirstListener);

convertView.setBackgroundColor(mSelectedItemsIds.get(position) ? mContext.getResources().getColor(R.color.listview_multi_sel_bg) : Color.TRANSPARENT);

break;
}
case TYPE_1: {
TextView newsTitleView = (TextView) holder.getView(R.id.list_item_title);

newsTitleView.setText(newsEntity.title);

if(mFavoriteFalg) {

} else {
if (newsEntity.images != null && newsEntity.images.size() >= 1) {

newsImageView.setVisibility(View.VISIBLE);
mImageLoader.displayImage(newsEntity.images.get(0), newsImageView, mOptions, mAnimateFirstListener);
} else {
newsImageView.setVisibility(View.GONE);
}
newsTitleView.setTextColor( newsEntity.is_read ? mContext.getResources().getColor(titleReadColorId) : mContext.getResources().getColor(titleColorNorId) );
}

convertView.setBackgroundColor(mSelectedItemsIds.get(position) ? mContext.getResources().getColor(R.color.listview_multi_sel_bg) : Color.TRANSPARENT);

break;
}
case TYPE_1: {
case TYPE_2: {
TextView dateView = (TextView) holder.getView(R.id.date_text);
dateView.setText(ZhihuUtils.getDateTag(mContext, newsEntity.title));
break;
Expand Down

0 comments on commit 83e6802

Please sign in to comment.