import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.genesysci.leo.R;
import com.genesysci.leo.others.Child;
public class Expandable_adapter extends BaseExpandableListAdapter{
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private Context _context;
private ArrayList<String>_listDataHeader;
private HashMap<String, List<Child>> _listDataChild;
private HashMap<String, ArrayList<Child>> _listDataChildTemp=new HashMap<String, ArrayList<Child>>();
public Expandable_adapter(Context context, ArrayList<String> listDataHeader, HashMap<String, List<Child>> listDataChild) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listDataChild;
_listDataChildTemp.putAll(listChildData);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//final String childText = (String) getChild(groupPosition, childPosition);
Child child = (Child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_child, null);
}
TextView expandedListTextView = (TextView) convertView.findViewById(R.id.tv_listchild);
ImageView image = (ImageView) convertView.findViewById(R.id.img);
image.setImageBitmap(child.getImage());
ImageView image2 = (ImageView) convertView.findViewById(R.id.img2);
image2.setImageBitmap(child.getImage());
String toto = " ";
String toto1 = " ";
if (child.getName() != null) {
toto = child.getName();
}
else {
toto = "";
}
if (child.getSubject() != null) {
toto1 = child.getSubject();
}
else {
toto1 = "";
}
expandedListTextView.setText(Html.fromHtml(toto + " " + toto1));
//expandedListTextView.setText(Html.fromHtml(childText));
expandedListTextView.setMovementMethod(LinkMovementMethod.getInstance());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitle = (TextView) convertView.findViewById(R.id.tv_listtitle);
listTitle.setTypeface(null, Typeface.BOLD);
listTitle.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void filter(String text){
_listDataChild.clear();
HashSet<String> headerData=new HashSet<String>();
ArrayList<Child> testData=null;
if(text.isEmpty()){
_listDataChild.putAll(_listDataChildTemp);
}else{
for (Map.Entry<String, ArrayList<Child>> entry : _listDataChildTemp.entrySet()) {
String key = entry.getKey();
testData=new ArrayList<Child>();
if(testData.getName().toLowerCase().contains(text.toLowerCase())){
_listDataChild.put(key,testData);
}
}
}
notifyDataSetChanged();
}
}
所有孩子的身高都大于屏幕高度,那么 LinearLayoutManager.scrollToPositionWithOffset(pos, 0)
效果很好。但如果RecyclerView
所有孩子的身高都小于屏幕高度,那么它就不起作用。
我们说我有一个RecyclerView
和一个Activity
作为根视图。 RecyclerView
RecyclerView
和width
都是height
。此match_parent
有3个项目,这3个子视图的高度之和小于屏幕高度。我想在RecyclerView
为Activity
时隐藏第一项。用户将在开始时看到第二项。如果用户向下滚动,他仍然可以看到第一项。所以我打电话给onCreated
。但由于LinearLayoutManager.scrollToPositionWithOffset(1, 0)
所有孩子的身高都小于屏幕高度,因此无法奏效。
即使RecyclerView
所有孩子的身高都小于屏幕高度,我如何才能RecyclerView
滚动到特定位置。
根据@Haran Sivaram的答案,以下是我的代码:
RecyclerView
有效。但是有一些小问题。
答案 0 :(得分:0)
您需要做的是将回收者视图的高度增加到minimum height
,这将允许滚动并隐藏您的第一个元素(screen height + height of the first item)
。你可以通过添加一个虚拟元素作为最后一个元素并设置它的高度来实现这一点,或者你也可以使用填充/边距来做到这一点(没试过这个)。
这也需要在绘制视图后动态完成(如果你事先知道每个项目的大小,你可以静态地完成它 - 虽然我不会推荐这种方法。)
绘制视图后,使用onGLobalLayoutListner
获取callback
,在此处进行测量并更新高度。现在带偏移的滚动应该可以正常工作。