因此,我已經搜索了幾個星期的問題的答案或可能的解決方案,但仍然沒有得到任何進一步的解決。我正在製作一個應用程序,在某些事情之後用積分獎勵用戶。他們可以訪問的頁面之一是基於用戶擁有的朋友的排行榜。 我能夠實現排行榜,並根據他們的點數打印用戶,但無法實現條形圖樣式外觀。像這樣:http://imgur.com/tF51RsA (不得不張貼一個鏈接,因爲我不能粘貼在這裏的圖片)更改列表視圖中單個項目的寬度
這是我到目前爲止已經試過: 1.添加到XML,並試圖getLayoutParams自定義適配器然後設置寬度,這是行不通的。 2.使用onDraw在列表項上繪製一個矩形。
這裏是我的領導委員會的XML文件(或接近):
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/leader_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top"
android:background="#00000000">
</LinearLayout>
我的列表視圖行XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/user_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_default_user"/>
<TextView
android:id="@+id/rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:layout_toRightOf="@+id/user_image"/>
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="20dp"
android:textSize="16sp"
android:layout_toRightOf="@+id/rank"/>
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/user_name"
android:gravity="right"
android:paddingRight="15dp"/>
</RelativeLayout>
</FrameLayout>
,並在我的排行榜類我自定義ArrayAdapter
public class LeaderAdapter extends ArrayAdapter<LeaderboardDM>{
ArrayList<LeaderboardDM> leaders;
int layoutResourceId;
public LeaderAdapter(Context context, int layoutResourceId, ArrayList<LeaderboardDM> leaders){
super(context, layoutResourceId, leaders);
this.leaders = new ArrayList<LeaderboardDM>();
this.leaders = leaders;
this.layoutResourceId = layoutResourceId;
}
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
ViewHolder viewHolder = null;
if(v == null){
v = getLayoutInflater().inflate(layoutResourceId, null, false);
viewHolder = new ViewHolder();
viewHolder.userImage = (ImageView) v.findViewById(R.id.user_image);
viewHolder.rank = (TextView) v.findViewById(R.id.rank);
viewHolder.userName = (TextView) v.findViewById(R.id.user_name);
viewHolder.score = (TextView) v.findViewById(R.id.score);
v.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) v.getTag();
}
LeaderboardDM lead = leaders.get(position);
if(lead != null){
//doesn't set user image yet
viewHolder.userName.setText(lead.user);
viewHolder.score.setText(String.valueOf(lead.points));
viewHolder.rank.setText("#"+String.valueOf(position+1));
}
return v;
}
class ViewHolder{
ImageView userImage;
TextView rank, userName, score;
}
}
和排行榜DM類
public class LeaderboardDM{
public String user;
public int points;
public String profilePicUrl;
public void setUserName(String user){
this.user = user;
}
public String getUserName(){
return user;
}
public void setPoints(int points){
this.points = points;
}
public int getPoints(){
return points;
}
public void setProfilePic(String url){
this.profilePicUrl = url;
}
public String getProfilePicUrl(){
return profilePicUrl;
}
}
該列表通過使用比較器進行排序,然後再按打分的順序進行打印。如果任何人有關於如何創建這樣的東西的想法,請幫助指向正確的方向。
謝謝!
編輯: 感謝@Ridcully我能解決這個問題。 該解決方案對於將來可能會這樣做的任何人都非常適用,它創建一個自定義進度條並從資源中獲取drawable,然後將ProgressDrawable設置爲drawable。從那裏我會通過setProgress(int width)設置寬度。
由於LeaderboardDM的屬性是公開的,因此您可能會失去getter和setter並直接訪問它們。 – Ridcully 2014-10-27 18:34:08