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.
Here is my code for main Activity
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;
}
}
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" />
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);
Set onclickListener on this convertView and load more data on its click .
来源:https://stackoverflow.com/questions/34586214/load-earlier-messages-in-listview-like-whats-app