我在这里有一个代码,允许我将项目添加到顶部。如果我有5件物品,它的工作方式就是这样。用复选框将项目添加到列表视图顶部
0 New Item
1
2
3
4 First item
然后,如果有新东西被添加它就成了这个。
0 New Item
1
2
3
4
5 First Item
正如你所看到的,无论何时添加了一个新的项目,它成为位置0。这是一个问题,因为如果我设置复选框上的对位0,然后添加一个新的项目,新项目成为位置0,它检查新的项目并取消选中之前的项目。 我希望它能够像这样工作,同时仍然将项目添加到顶部。
4 New item
3
2
1
0 First Item
添加新项目时。
5 New Item
4
3
2
1
0 First Item
这是我的ListView适配器。
public class TweetArrayAdapter extends ArrayAdapter<Object> implements OnClickListener {
TweetList tweetMessageObj;
ViewHolder holder = null;
public List<TweetList> tweetList = new ArrayList<TweetList>();
public void add(TweetList object,int position) {
tweetList.add(position,object);
}
public TweetArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public int getCount() {
return this.tweetList.size();
}
public TweetList getItem(int position) {
return tweetList.get(position);
}
@SuppressLint("ViewHolder")
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
tweetMessageObj = getItem(position);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.tweet_list_item, parent, false);
holder = new ViewHolder();
holder.twitterUser = (TextView)row.findViewById(R.id.display_name);
holder.tweet = (TextView) row.findViewById(R.id.display_tweet);
holder.twitterMention = (TextView)row.findViewById(R.id.display_twitter_mentionname);
holder.profile_picture = (ImageView)row.findViewById(R.id.profile_picture);
holder.tweet_picture = (ImageView)row.findViewById(R.id.tweet_image);
holder.padding = (View)row.findViewById(R.id.view1);
holder.favorite = (CheckBox)row.findViewById(R.id.favorite_button);
//holder.retweet = (CheckBox)row.findViewById(R.id.retweet_button);
//holder.reply = (CheckBox)row.findViewById(R.id.reply_button);
holder.favorite.setTag(holder);
row.setTag(holder);
}else{
holder = (ViewHolder)row.getTag();
}
SpannableString hashtag = new SpannableString(tweetMessageObj.tweet);
Matcher matcher = Pattern.compile("@([A-Za-z0-9_-]+)").matcher(hashtag);
Matcher matcher2 = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashtag);
while (matcher.find())
{
hashtag.setSpan(new ForegroundColorSpan(Color.rgb(79, 120, 216)), matcher.start(), matcher.end(), 0);
hashtag.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
TextView tv = (TextView)widget;
String tags = tv.getText().subSequence(tv.getSelectionStart(),tv.getSelectionEnd()).toString();
Toast.makeText(getActivity(), tags, Toast.LENGTH_LONG).show();
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
},matcher.start(), matcher.end(), 0);
}
while (matcher2.find())
{ hashtag.setSpan(new ForegroundColorSpan(Color.rgb(79, 120, 216)), matcher2.start(), matcher2.end(), 0);
hashtag.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
TextView tv = (TextView)widget;
String tags = tv.getText().subSequence(tv.getSelectionStart(),tv.getSelectionEnd()).toString();
Toast.makeText(getActivity(), tags, Toast.LENGTH_LONG).show();
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
},matcher2.start(), matcher2.end(), 0);
}
holder.tweet.setText(hashtag, BufferType.SPANNABLE);
holder.tweet.setMovementMethod(LinkMovementMethod.getInstance());
holder.tweet.setHighlightColor(Color.TRANSPARENT);
if(tweetMessageObj.tweet.isEmpty()){
holder.tweet.setVisibility(View.GONE);
}
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/light.ttf");
holder.tweet.setTypeface(tf);
Typeface tf2 = Typeface.createFromAsset(getActivity().getAssets(), "fonts/bold.ttf");
holder.twitterUser.setText(tweetMessageObj.twittername);
holder.twitterUser.setTypeface(tf2);
holder.twitterMention.setText("@" + tweetMessageObj.mentionname);
Picasso.with(getActivity()).load(tweetMessageObj.pictureURL).into(holder.profile_picture);
if(tweetMessageObj.tweetPictureUrl != null){
holder.tweet_picture.setVisibility(View.VISIBLE);
holder.padding.setVisibility(View.VISIBLE);
Picasso.with(getActivity()).load(tweetMessageObj.tweetPictureUrl).into(holder.tweet_picture);
}else{
holder.tweet_picture.setVisibility(View.GONE);
holder.padding.setVisibility(View.GONE);
}
return row;
}
}
public static class ViewHolder{
TextView tweet,twitterUser,twitterMention;
ImageView profile_picture,tweet_picture;
CheckBox reply,retweet,favorite;
View padding;
boolean isFavorited = false;
}
TweetList
public class TweetList {
public String twittername,mentionname,tweet,pictureURL,tweetPictureUrl;
long status_id;
public TweetList(long status_id,String twittername,String mentionname,String tweet,String pictureURL) {
super();
this.status_id = status_id;
this.twittername = twittername;
this.tweet = tweet;
this.pictureURL = pictureURL;
this.mentionname = mentionname;
}
public TweetList(long status_id,String twittername,String mentionname,String tweet,String pictureURL,String tweetPictureUrl) {
super();
this.status_id = status_id;
this.twittername = twittername;
this.tweet = tweet;
this.pictureURL = pictureURL;
this.mentionname = mentionname;
this.tweetPictureUrl = tweetPictureUrl;
}
}
后TweetList代码。 – 2014-10-09 12:58:47
我编辑了帖子。 – Rhynoboy2009 2014-10-09 13:00:46