Android:如何在listView中设置特定行的背景,而不更改先前的条目

时间:2013-10-15 13:46:52

标签: android android-listview baseadapter

我正在尝试制作聊天程序。我的问题是,每次背景图像更新(发件人更改)时,阵列中的每个项目都会获得相同的背景。我怎样才能改变这一点,以便传出的消息得到一种类型的背景,传入的消息又得到另一种?

    public class RowAdapter extends BaseAdapter {

private Context context;
private int textViewResourceId;
private final ArrayList<String> messageList;
private final ArrayList<String> senderList;
public String messageDirection; 
boolean out; 

public RowAdapter(Context context, int textViewResourceId, ArrayList<String> messageList, ArrayList<String> senderList) {
super();

this.textViewResourceId = textViewResourceId;
this.messageList = messageList; 
this.senderList = senderList; 
this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){ 
View rowView = convertView;
ViewHolder holder;
long timestamp = System.currentTimeMillis();
String readableTimestamp = (String)DateUtils.getRelativeTimeSpanString(timestamp);

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(textViewResourceId, parent, false);

    holder = new ViewHolder();
    holder.label = (TextView)rowView.findViewById(R.id.label);
    holder.message=(TextView)rowView.findViewById(R.id.message);
    holder.time = (TextView)rowView.findViewById(R.id.textTime);
    holder.RL = (RelativeLayout)rowView.findViewById(R.id.userAndMessage);
    rowView.setTag(holder);
}else{
    holder = (ViewHolder)rowView.getTag();
}

holder.label.setText(senderList.get(position));
holder.message.setText(messageList.get(position));
holder.time.setText(readableTimestamp);

if(out){
    if (messageList.get(position)!= null) {
        holder.RL.setBackgroundResource(R.drawable.yellow);
       ((RelativeLayout) rowView).setGravity(Gravity.RIGHT);
    }

}else{
    if (messageList.get(position)!= null) {
        holder.RL.setBackgroundResource(R.drawable.green);
       ((RelativeLayout) rowView).setGravity(Gravity.LEFT);
    }
}
return rowView;
}

public void setOut(boolean out){
this.out = out; 
}
static class ViewHolder{
RelativeLayout RL; 
TextView label;
TextView message;
TextView time;

}
@Override
public int getCount() {
    return messageList.size();
}

@Override 
public Object getItem(int position) {
    return position;
}

 @Override
public long getItemId(int position) {
    return position;
}

}

1 个答案:

答案 0 :(得分:2)

好的,试着这样做

这只是一个例子,您必须在其中添加您的流程

  

主要布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView_Chat"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:background="#123456"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="fromMeBt"
            android:text="From me" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="fromOtherBt"
            android:text="From other" />
    </LinearLayout>

</LinearLayout>
  

添加列表行xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/txt_chat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
  

创建一个名为ChatObject的新类

public class ChatObject {

    private String mssg;
    private int from;

    public ChatObject(String mssg, int from) {
        super();
        this.mssg = mssg;
        this.from = from;
    }

    public String getMssg() {
        return mssg;
    }

    public void setMssg(String mssg) {
        this.mssg = mssg;
    }

    public int getFrom() {
        return from;
    }

    public void setFrom(int from) {
        this.from = from;
    }

}
  

现在,像这样对您的MainActivity进行一些更改

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ArrayList<ChatObject> items = new ArrayList<ChatObject>();
    ListView listView_chat;
    CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new CustomAdapter(items);
        listView_chat = (ListView) findViewById(R.id.listView_Chat);
        listView_chat.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void fromMeBt(View v) {
        ChatObject object = new ChatObject("from me :)", 0);
        items.add(object);
        adapter.notifyDataSetChanged();
        listView_chat.setSelection(items.size());
    }

    public void fromOtherBt(View v) {
        ChatObject object = new ChatObject("from other :)", 1);
        items.add(object);
        adapter.notifyDataSetChanged();
        listView_chat.setSelection(items.size());
    }

    class CustomAdapter extends BaseAdapter {

        ArrayList<ChatObject> items;

        public CustomAdapter(ArrayList<ChatObject> items) {
            // TODO Auto-generated constructor stub
            this.items = items;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_chat, null);
            TextView txt_chat = (TextView) convertView.findViewById(R.id.txt_chat);

            ChatObject object = items.get(position);
            txt_chat.setText(object.getMssg());

            if (object.getFrom() == 0) {
                convertView.setBackgroundColor(Color.BLUE);
            }else {
                convertView.setBackgroundColor(Color.CYAN);
            }

            return convertView;
        }

    }
}

就是这样,如果你想在xml布局中更改填充或将其添加到dimens.xml文件

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>
现在开心吗?我希望这能帮到你