我想在加载活动后滚动到RecyclerView列表的底部.
GENERIC_MESSAGE_LIST = (ArrayList) intent.getExtras().getParcelableArrayList(ConversationsAdapter.EXTRA_MESSAGE);
conversationView = (RecyclerView) findViewById(R.id.list_messages);
conversationView.setHasFixedSize(true);
conversationViewLayoutManager = new LinearLayoutManager(this);
conversationView.setLayoutManager(conversationViewLayoutManager);
conversationViewAdapter = new ConversationAdapter(GENERIC_MESSAGE_LIST, this);
conversationView.setAdapter(conversationViewAdapter);
Run Code Online (Sandbox Code Playgroud)
conversationView.scrollTo(...)抛出了一个关于在RecyclerView中不受支持的例外,并且conversationView.scrollToPosition(...)似乎没有做任何事情.
在上面的代码块之后,我补充道
conversationView.scrollToPosition(GENERIC_MESSAGE_LIST.size() + 1)
Run Code Online (Sandbox Code Playgroud)
这不起作用.有30个元素GENERIC_MESSAGE_LIST.
所以我在尝试设置视图的背景可绘制时有点混乱.该代码依赖于知道视线的高度,所以我不能把它onCreate()或onResume(),因为getHeight()返回0 onResume()似乎是最接近我可以得到虽然.我应该在哪里放置如下所示的代码,以便在显示给用户时背景发生变化?
TextView tv = (TextView)findViewById(R.id.image_test);
LayerDrawable ld = (LayerDrawable)tv.getBackground();
int height = tv.getHeight(); //when to call this so as not to get 0?
int topInset = height / 2;
ld.setLayerInset(1, 0, topInset, 0, 0);
tv.setBackgroundDrawable(ld);
Run Code Online (Sandbox Code Playgroud) 我正在尝试确定一些UI元素的实际像素尺寸!
这些元素从.xml文件中膨胀,并使用dip宽度和高度进行初始化,以便GUI最终支持多个屏幕大小和dpi(如android规范所推荐).
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="150dip"
android:orientation="vertical">
<ImageView
android:id="@+id/TlFrame"
android:layout_width="110dip"
android:layout_height="90dip"
android:src="@drawable/timeline_nodrawing"
android:layout_margin="0dip"
android:padding="0dip"/></LinearLayout>
Run Code Online (Sandbox Code Playgroud)
此前的xml表示一帧.但是我在这里描述的水平布局中动态添加了许多:
<HorizontalScrollView
android:id="@+id/TlScroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="0dip"
android:padding="0dip"
android:scrollbars="none"
android:fillViewport="false"
android:scrollbarFadeDuration="0"
android:scrollbarDefaultDelayBeforeFade="0"
android:fadingEdgeLength="0dip"
android:scaleType="centerInside">
<!-- HorizontalScrollView can only host one direct child -->
<LinearLayout
android:id="@+id/TimelineContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_margin="0dip"
android:padding="0dip"
android:scaleType="centerInside"/>
</HorizontalScrollView >
Run Code Online (Sandbox Code Playgroud)
定义为在我的java代码中添加一个框架的方法:
private void addNewFrame()
{
LayoutInflater inflater = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
TextView frameNumber = (TextView) root.findViewById(R.id.FrameNumber);
Integer val = new Integer(_nFramesDisplayed+1); //+1 …Run Code Online (Sandbox Code Playgroud) android android-widget android-ui android-layout android-xml
我需要监听确切的一个全局布局事件,以初始设置正确的滚动位置.在搜索了一些为什么我的调用scrollTo(x,y)似乎被忽略之后,我发现只有在知道整个布局后才能以有意义的方式调用它们.所以我正在注册GlobalLayoutListener并推迟致电scrollTo().
问题是,我只想滚动一次.所以我想我可以简单地打电话removeGlobalOnLayoutListener()来停止听.这导致了例外:IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again.所以我认为我很好,如果观察者不活着它将不会发生任何事件.但遗憾的是:每当布局以某种方式改变时,我的视图都会滚动.
我当前的代码迭代看起来像这样.我可以更改什么以确保scrollToGridPos()只进行一次调用?我知道我可以mHasFired 在内部类中添加一个本地字段,但这对我来说似乎非常脏!
final ViewTreeObserver vto = mLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
scrollToGridPos(getCenterPoint(), false);
if (vto.isAlive()) {
vto.removeGlobalOnLayoutListener(this);
}
}
});
Run Code Online (Sandbox Code Playgroud) 我在StackOverflow中多次看到过这个问题,但我无法解决问题.所以伸出援助之手.请注意:我已经检查了其他问题,虽然我可以摆脱错误,但还有其他问题.
这是我现有的代码:
我的适配器类:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private IOnLoadMoreListener mIOnLoadMoreListener;
private int visibleThreshold = 3;
private int lastVisibleItem, totalItemCount;
private int[] lastVisibleItems = new int[mStaggeredLayoutManager.getSpanCount()];
public RecyclerViewAdapter(Context context) {
this.context = context;
final StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = staggeredGridLayoutManager.getItemCount();
lastVisibleItems = staggeredGridLayoutManager.findLastVisibleItemPositions(new int[mStaggeredLayoutManager.getSpanCount()]);
if (staggeredGridLayoutManager.getSpanCount() == …Run Code Online (Sandbox Code Playgroud)