在Whats App之类的ListView中加载较早的消息 [英] Load Earlier Messages in listview like whats app

查看:78
本文介绍了在Whats App之类的ListView中加载较早的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击加载较早的消息"时,较早的消息已从数据库中加载,但是列表视图滚动到顶部(尚未完成),该消息应仅加载到列表视图的顶部而不滚动.

When i Click load earlier messages the earlier messages get loaded from database but list view scrolls to the top which is not be done , the message should only loaded at the top of list view without its scroll.

这是我主要活动的代码

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.private_group_chat);

    Intent i = getIntent();
    btnLoadMore = new Button(this);
    btnLoadMore.setText("Load Earlier Messages");
    lsvChat = (ListView) findViewById(R.id.lv_messsages);
    lsvChat.addHeaderView(btnLoadMore);
    chatModel = new ArrayList<ChatModel>();
    db = new Database3(getApplicationContext());
    chatModel = db.getSelectedIncidentDetails(getApplicationContext(),
            groupId, count);
    chatAdapter = new ChatAdapterGroup(getApplicationContext(), chatModel);
    lsvChat.setAdapter(chatAdapter);
    discoverModel = new ArrayList<DiscoverModel>();
    dm = new DiscoverModel();

}

这是我的基本适配器...

here is my base adapter......

public class ChatAdapterGroup extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    int count = 0;
    private ArrayList<ChatModel> list;

    public ChatAdapterGroup(Context context, ArrayList<ChatModel> list) {
        this.context = context;
        this.list = list;

        inflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    private class ViewHolder {
        TextView txt_left;
        TextView txt_right;
        TextView tv_fromName;
        RelativeLayout rl_left;
        RelativeLayout rl_right;
    }

    @Override
    public View getView(final int position, View v, ViewGroup arg2) {

        final ViewHolder holder;
        if (v == null) {

            holder = new ViewHolder();
            v = inflater.inflate(R.layout.chat_adapter, null);

            holder.txt_left = (TextView) v.findViewById(R.id.txt_left);

            holder.txt_right = (TextView) v.findViewById(R.id.txt_right);

            holder.tv_fromName = (TextView) v.findViewById(R.id.tv_fromName);

            holder.rl_left = (RelativeLayout) v.findViewById(R.id.rl_left);

            holder.rl_right = (RelativeLayout) v.findViewById(R.id.rl_right);

            v.setTag(holder);

        } else {
            holder = (ViewHolder) v.getTag();

        }

        if (list.get(position) != null) {

            if (list.get(position).getGroup_cordinate()
                    .equalsIgnoreCase("left")) {
                holder.txt_left.setText(""
                        + list.get(position).getGroup_messages_recive());
                holder.tv_fromName.setText(""
                        + list.get(position).getFrom_name_recive());
                holder.txt_left.setTextColor(Color.parseColor("#000000"));
                holder.rl_right.setVisibility(View.GONE);
                holder.rl_left.setVisibility(View.VISIBLE);
            } else {
                holder.txt_right.setText(""
                        + list.get(position).getGroup_messages_recive());
                holder.rl_left.setVisibility(View.GONE);
                holder.rl_right.setVisibility(View.VISIBLE);

            }

        }

        v.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // notifyDataSetChanged();
            }
        });
        return v;
    }

}

和列表视图的xml编码.............

and the xml coding for List view .............

<ListView
        android:id="@+id/lv_messsages"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ll_send"
        android:layout_below="@+id/rl_header"
        android:divider="@null"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll" />

推荐答案

您只需要一个名为加载更多"的视图.并将其添加为列表视图的标题,然后单击以从数据库中获取数据并使用notifyDatsetChanged().就是这样.

You just need a view which called "Load more " . And add it as header of list view and on its click fetch the data from database and use notifyDatsetChanged(). thats it .

  LayoutInflater  inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
convertViewHeader = inflater.inflate(R.layout.row_idea_list , null);
convertViewHeader.setBackgroundColor(getResources().getColor(R.color.gray_light_light));

listView.addHeaderView(convertViewHeader, null, false);

在此convertView上设置onclickListener并在其click上加载更多数据.

Set onclickListener on this convertView and load more data on its click .

这篇关于在Whats App之类的ListView中加载较早的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆