我的应用程序中有很多行我觉得没有得到应有的优化;listView 滚动在某些设备上不稳定。我玩过相对布局和相同的效果。我在代码下方发布了最大的罪犯。
注意,每一行都是一个非常复杂的布局,Typefaces
也使用自定义。我发布XML
和adapter
(getView()
)代码:
有人可以看看这个并让我知道我是否在做一些不好的做法吗?(我尽量不要嵌套布局,但有时你必须这样做!)
这是您在视觉上看到的内容。结果是我想要的;只是不平滑的滚动条:
这是 XML:
<?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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:background="@drawable/following_home_selector"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rlCommentButtonBar"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@drawable/following_header_selector"
android:orientation="horizontal" >
<ImageView
android:id="@+id/labelFeaturedProfilePic"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="10dp"
android:background="#bbb"
android:contentDescription="Profile"
android:src="@drawable/usericon" />
<LinearLayout
android:layout_width="1px"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/llFollowingCommentButton"
android:background="#bbb" >
</LinearLayout>
<LinearLayout
android:id="@+id/llFollowingCommentButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/review_button_selector"
android:orientation="vertical" >
<TextView
android:id="@+id/tvFollowingCommentLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="3dp"
android:text="comment"
android:textColor="#888" />
<TextView
android:id="@+id/tvFollowingCommentTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="-2dp"
android:background="@drawable/ic_comment_gray"
android:gravity="center_horizontal"
android:padding="1dp"
android:text="8"
android:textColor="#fff"
android:textSize="12sp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/labelFeaturedProfilePic"
android:orientation="vertical" >
<TextView
android:id="@+id/myMastCat_Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:textColor="#222"
android:textSize="20sp" />
<TextView
android:id="@+id/myMastCat_Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textColor="#999"
android:textSize="12sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/following_inner_box_selector"
android:orientation="vertical"
android:padding="7dp" >
<LinearLayout
android:id="@+id/llWatchItemCommentButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/myMasterCat_Item"
style="@style/DropShadowEffect"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:layout_weight="90"
android:shadowColor="#ffffff"
android:text="Item"
android:textColor="#666"
android:textSize="24sp" />
</LinearLayout>
<TextView
android:id="@+id/myMastCat_Cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="-3dp"
android:gravity="top"
android:text="MasterCat"
android:textColor="#666"
android:textSize="14sp" />
<TextView
android:id="@+id/myMasterCat_Review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:text="This is an item review."
android:textColor="#666"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
至于图层/过度绘制:
我的主题背景是#d8d9d9 hex;包含listView
(未显示)的布局文件没有背景。
这是我的 ListView(第三方库)
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_to_refresh_scrollview_feat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#00000000" />
我不得不使用00000000
删除默认的蓝色背景,由于某种原因,它只在深灰色背景下出现在按下状态。
您看到的配置文件图像是使用LazyList
库加载的。
另外,请注意我使用了三种不同的 Roboto Typeface
;并且除了一个 textView 之外的所有内容都在使用自定义TypeFace
.
最后,这是我getView()
的adapter
:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.master_cat_following, null,
true);
holder = new ViewHolder();
holder.i1 = (ImageView) convertView
.findViewById(R.id.labelFeaturedProfilePic);
holder.tv1 = (TextView) convertView
.findViewById(R.id.myMastCat_Cat);
holder.tv2 = (TextView) convertView
.findViewById(R.id.myMasterCat_Item);
holder.tv3 = (TextView) convertView
.findViewById(R.id.myMastCat_Username);
holder.tv3.setTypeface(roboto_regular);
holder.tv4 = (TextView) convertView
.findViewById(R.id.myMasterCat_Review);
holder.tv6 = (TextView) convertView
.findViewById(R.id.myMastCat_Date);
holder.tv7 = (TextView) convertView
.findViewById(R.id.tvFollowingCommentTotal);
holder.tv4.setTypeface(roboto_light);
holder.tv8 = (TextView) convertView
.findViewById(R.id.tvFollowingCommentLabel);
holder.tv8.setTypeface(roboto_thin);
holder.l1 = (LinearLayout) convertView
.findViewById(R.id.llFollowingCommentButton);
holder.r1 = (RelativeLayout) convertView
.findViewById(R.id.rlCommentButtonBar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final FollowingItems mco = getItem(position);
final String item = mco.getItem();
String cat = mco.getCat();
final String user = mco.getUser();
String date = mco.getDate();
date = DateConvert.dateConvert(Long.valueOf(date));
long commentTotal = mco.getCommentCounts();
String review = mco.getReview();
String url = mco.getUrl();
holder.tv1.setTypeface(roboto_light);
holder.tv2.setTypeface(bpReplay);
holder.tv1.setText(cat);
holder.tv2.setText(item);
holder.tv3.setText(user);
holder.tv4.setText(review);
holder.tv6.setText(date);
Drawable dBlue = (Drawable) getContext().getResources().getDrawable(
R.drawable.ic_comment_blue);
Drawable dGray = (Drawable) getContext().getResources().getDrawable(
R.drawable.ic_comment_gray);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
if (commentTotal == 0) {
holder.tv7.setBackgroundDrawable(dGray);
} else {
holder.tv7.setBackgroundDrawable(dBlue);
}
} else {
if (commentTotal == 0) {
holder.tv7.setBackground(dGray);
} else {
holder.tv7.setBackground(dBlue);
}
}
holder.tv7.setText(String.valueOf(commentTotal));
if (!url.equals("http://www.rate-it-web.com/profile_pics/null")) {
imageLoader.DisplayImage(url, holder.i1);
} else {
holder.i1.setImageResource(R.drawable.usericon);
}
holder.l1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FollowingItems mco = getItem(position);
Intent intent = new Intent(getContext(), CommentActivity.class);
// edited out
intent.putExtra("userRated", String.valueOf(mco.getUserRated()));
getContext().startActivity(intent);
}
});
holder.r1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FollowingItems mco = getItem(position);
Intent i = new Intent(getContext(), ProfileActivity.class);
i.putExtra("userprofile", mco.getUser());
i.putExtra("userIdprofile", String.valueOf(mco.getUserId()));
getContext().startActivity(i);
}
});
return convertView;
}