gpt4 book ai didi

android - 在保持位置的同时将列表项添加到顶部

转载 作者:行者123 更新时间:2023-11-30 02:37:27 25 4
gpt4 key购买 nike

我正在尝试将新的 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;
}

最佳答案

我想我明白你想要达到的目的......一个更简单的方法是简单地在特定索引处添加你的新项目。试试这个:

public void add(TweetList object, int index) {
tweetList.add(index, object);
super.add(index, object);
}

我认为如果您这样做,您将不必在 getItem() 方法中做所有额外的工作。也可以改为:

TweetList getItem(int position) {
return tweetList.get(position);
}

如果您的 List 已经按照您希望它出现在 ListView 中的顺序排列,那么您要做的工作就会减少,并且出错的可能性也会降低。希望我理解正确,希望这对您有所帮助!

编辑:

保留一个变量,该变量保存应添加下一个项目的位置的索引..

    private int index = 0;

public void add(TweetList object) {
tweetList.add(index, object);
index++;
}

这样做会将第一个新项目添加到列表顶部,任何后续项目都将添加到最近添加的新项目之后。

关于android - 在保持位置的同时将列表项添加到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26258963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com