如何更新ListView条目并使用户继续并滚动ListView值?

时间:2019-05-29 17:38:18

标签: android android-listview

我正在尝试在用户滚动列表时更新listView的列表,并为用户提供继续和滚动新条目的选项,但是没有成功,我需要帮助。

在我的应用程序中,我正在聊天,并且正在SQLite中保存聊天的历史记录。当用户进入聊天时,应用程序仅读取最后25条消息,并在列表底部显示最新消息。 为了做到这一点,我在XML上使用了以下选项:

    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"

,它工作正常。 现在,当用户开始向上滚动并查看聊天记录时,当他到达列表顶部时,我正在访问数据库并检索另外25条消息,并将它们添加到列表顶部,并且打电话给:         chatAdaptor.notifyDataSetChanged();

但是当用户试图向上滚动时,什么也没有呈现,只有在向下和向上滚动之后,才会显示新条目。

我已经尝试使用setSelection和smoothScrollToPosition设置列表视图的位置,但这确实不起作用。

如何使用新条目更新列表,并为用户提供继续滚动和向上滚动的选项?

这是相关代码:

<ListView
    android:id="@+id/chatList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:layout_margin="2dp"
    android:layout_weight="1"
    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"
    android:background="@drawable/frame_tab"></ListView>


public void showChatLines() {

    chatLinesView = chatTextDialog.findViewById(R.id.chatList);

    chatAdaptor = new GenericAdapter<ChatLine>(activity, chatLines) {
        @Override
        public View getMyView(int position, View convertView, ViewGroup parent, ChatLine chatLine) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.chat_line, null);

            TextView chatLineText = view.findViewById(R.id.chatLine);
            chatLineText.setText(chatLine.messageText);
            TextView chatLineTime = view.findViewById(R.id.chatLineTime);
            chatLineTime.setText(chatLine.displayDateTime);
            TextView chatLineUserName = view.findViewById(R.id.userName);
            chatLineUserName.setText(chatLine.userDisplayName);
            chatLineUserName.setTextColor(activity.getColor(Constants.colors[getColorBy(chatLine.userName)]));

            LinearLayout chatLineLayout = view.findViewById(R.id.chatLineLayout);
            LinearLayout.LayoutParams chatLineLayoutParams = (LinearLayout.LayoutParams) chatLineLayout.getLayoutParams();
            CardView chatLineBackground = view.findViewById(R.id.chatLineBackground);
            LinearLayout.LayoutParams chatLineBackgroundParams = (LinearLayout.LayoutParams) chatLineBackground.getLayoutParams();
            LinearLayout chatLineSubLayout = view.findViewById(R.id.chatLineSubLayout);
            FrameLayout.LayoutParams chatLineSubLayoutParams = (FrameLayout.LayoutParams) chatLineSubLayout.getLayoutParams();

            if (chatLine.userName.equals(logonUserName)) {
                CardView charLineCard = view.findViewById(R.id.chatLineBackground);
                charLineCard.setCardBackgroundColor(activity.getColor(R.color.focusedColor));
                ((ViewGroup) chatLineUserName.getParent()).removeView(chatLineUserName);
            } else {
                chatLineLayoutParams.gravity = Gravity.RIGHT;
                chatLineLayout.setLayoutParams(chatLineLayoutParams);
                chatLineSubLayoutParams.gravity = Gravity.RIGHT;
                chatLineSubLayout.setLayoutParams(chatLineSubLayoutParams);
                chatLineBackgroundParams.gravity = Gravity.RIGHT;
                chatLineBackground.setLayoutParams(chatLineBackgroundParams);
            }

            if (position==0 & fromLine!=0) {
                getNextChatLinesFromDb(position);
            }
            return view;
        }
    };

    chatLinesView.setAdapter(chatAdaptor);

}

private void getNextChatLinesFromDb(int position) {
    toLine = fromLine - 1;
    fromLine -= pageSize;
    if (fromLine < 0) {
        fromLine = 0;
    }
    ArrayList<ChatLine> newChatLines = db.getChatLines(chatRoomId, fromLine, toLine);
    chatLines.addAll(0, newChatLines);
    chatAdaptor.notifyDataSetChanged();
}

2 个答案:

答案 0 :(得分:0)

我认为这段代码将为您提供帮助:

// get position of listview
int index = chatLinesView.getFirstVisiblePosition();
View v = chatLinesView.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

// notify dataset changed or re-assign adapter here

//re-assign the position of listview after setting the adapter again
chatLinesView.setSelectionFromTop(index, top);

修改

int h1 = chatLinesView.getHeight();
int h2 = chatLinesViewRow.getHeight();

chatLinesView.smoothScrollToPositionFromTop(position, h1/2 - h2/2, duration);
  

参数   位置->滚动到的位置

     

offset ---->距像素顶部所需位置的距离(以像素为单位)   滚动完成后查看

     

duration->用于滚动的毫秒数

答案 1 :(得分:0)

请尝试以下示例代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<ListView
    android:id="@+id/listView"
    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener{

final static private int ADD_DATA_SIZE = 10;

ArrayList<String> dataList;
ListView listView;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dataList = new ArrayList<>();
    for(int i=0; i<30; i++){
        dataList.add("Item " + i);
    }
    listView = findViewById(R.id.listView);
    adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, dataList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getApplicationContext(), adapterView.getItemAtPosition(i) + " Clicked!", Toast.LENGTH_SHORT).show();
        }
    });
    listView.setOnScrollListener(this);
}

private void addNewData() {
    ArrayList<String> tmpList = new ArrayList<>();
    for(int i=0; i<ADD_DATA_SIZE; i++){
        tmpList.add("Add " + (dataList.size() - 20)/10 + ": Item " + i);
    }
    dataList.addAll(0, tmpList);
    listView.setSelection(ADD_DATA_SIZE);
    adapter.notifyDataSetInvalidated();
}

@Override
public void onScrollStateChanged(AbsListView absListView, int i) {}

@Override
public void onScroll(AbsListView absListView, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    if(firstVisibleItem == 0){
        View view = absListView.getChildAt(0);
        if(view != null){
            if (view.getTop() == absListView.getTop()) {
                absListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_DISABLED);
                Toast.makeText(getApplicationContext(), "TRANSCRIPT MODE CHANGED TO: "
                        + absListView.getTranscriptMode(), Toast.LENGTH_SHORT).show();
                addNewData();
            }
        }
    } else if (totalItemCount == (firstVisibleItem + visibleItemCount)){
        View view = absListView.getChildAt(visibleItemCount - 1);
        if (view.getBottom() == absListView.getBottom()) {
            absListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
            Toast.makeText(getApplicationContext(), "TRANSCRIPT MODE CHANGED TO: "
                    + absListView.getTranscriptMode(), Toast.LENGTH_SHORT).show();
        }
    }
}
}

希望有帮助!