我正在尝试将新的Listview项目添加到顶部,同时保持与添加新项目相同的位置。我试过用这个。
TweetList getItem(int position) {
return this.tweetList.get(tweetList.getSize() - 1 - position);
}
但它始终将顶部位置更改为0.如何在不妨碍项目位置的情况下将项目添加到顶部?
public class TweetArrayAdapter extends ArrayAdapter<Object> implements OnClickListener {
TextView tweet,twitterUser,twitterMention;
ImageView profile_picture,tweet_picture;
View padding;
TweetList tweetMessageObj;
ViewHolder holder = null;
public List<TweetList> tweetList = new ArrayList<TweetList>();
public void add(TweetList object) {
tweetList.add(object);
super.add(object);
}
public TweetArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public int getCount() {
return this.tweetList.size();
}
public TweetList getItem(int position) {
return this.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 = (ImageView)row.findViewById(R.id.favorite_button);
holder.retweet = (ImageView)row.findViewById(R.id.retweet_button);
holder.reply = (ImageView)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 class TweetArrayAdapter extends ArrayAdapter<Object> implements OnClickListener {
TextView tweet,twitterUser,twitterMention;
ImageView profile_picture,tweet_picture;
View padding;
TweetList tweetMessageObj;
ViewHolder holder = null;
public List<TweetList> tweetList = new ArrayList<TweetList>();
int index = 0;
public void add(TweetList object) {
tweetList.add(index, object);
index++;
}
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 = (ImageView)row.findViewById(R.id.favorite_button);
holder.retweet = (ImageView)row.findViewById(R.id.retweet_button);
holder.reply = (ImageView)row.findViewById(R.id.reply_button);
holder.favorite.setTag(holder);
row.setTag(holder);
}else{
holder = (ViewHolder)row.getTag();
}
holder.favorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
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;
}
答案 0 :(得分:1)
我想我明白你要做的是什么...更简单的方法就是简单地在特定索引处添加新项目。试试这个:
public void add(TweetList object, int index) {
tweetList.add(index, object);
super.add(index, object);
}
我认为如果你这样做,你就不必在你的getItem()
方法中完成所有额外的工作。这也可以改为:
TweetList getItem(int position) {
return tweetList.get(position);
}
如果您的列表已经按照您希望它在ListView中显示的顺序排列,那么您可以减少工作量,并且不会出现错误。希望我理解正确,希望这有帮助!
编辑:
保留一个变量,该变量包含应添加下一项的位置的索引..
private int index = 0;
public void add(TweetList object) {
tweetList.add(index, object);
index++;
}
执行此操作会将第一个新项目添加到列表顶部,并且任何后续项目都将添加到最近添加的新项目后面。
答案 1 :(得分:0)
你必须操纵listview
// before adding new elements
int currentPosition = mList.getFirstVisiblePosition();
View child = mList.getChildAt(0);
int top = child == null ? 0 : child.getTop();
//TODO add new elements
mList.setSelectionFromTop(currentPosition + numberOfNewElements, top);