我有一个活动,其中有5个片段,每个片段都有recyclerview ...我想在我的活动中按住片段按钮,当我点击按钮时,回收者视图开始滚动到顶部...我得到了滚动部分,从片段中我可以很容易地做到但是如何在活动中获取片段recyclelerview ...请帮助
Activity.java
package com.example.khaalijeb.newlistview_module;
import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Search extends AppCompatActivity {
ImageView navbut;
ViewPager pager;
ViewPager mPager;
TabLayout mTabLayout;
SearchPagerAdapter mAdapter;
SlidingTabLayout1 tabs;
FloatingActionButton fab2;
String title[] = {"All", "Restaurant", "Wellness", "Fashion", "Others"};
int numoftab = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
ImageView back = (ImageView)findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
TextView location = (TextView)findViewById(R.id.location);
location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i =new Intent(Search.this,locationsearch.class);
startActivity(i);
}
});
ImageView locationlogo = (ImageView)findViewById(R.id.locationlogo);
locationlogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Search.this,locationsearch.class);
startActivity(i);
}
});
Button b = (Button)findViewById(R.id.click);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
mAdapter = new SearchPagerAdapter(getSupportFragmentManager(),title,numoftab);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mTabLayout.setTabsFromPagerAdapter(mAdapter);
mTabLayout.setupWithViewPager(mPager);
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Fragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("hii", "oncreate");
View view;
view = inflater.inflate(R.layout.activity_all, container, false);
NO_MERCHANT = (TextView) getActivity().findViewById(R.id.no_merchant);
font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/cae.ttf");
mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
final LinearLayoutManager l = new LinearLayoutManager(c);
mRecyclerView.setLayoutManager(l);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
// mAdapter.setClickListener(this);
page = 0;
preLast = 0;
adapterinitialised = false;
final FloatingActionButton fab2 = (FloatingActionButton)view.findViewById(R.id.fab2);
fab2.setVisibility(View.INVISIBLE);
fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecyclerView.smoothScrollToPosition(0);
}
});
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy != 0) {
Log.i("RecyclerView scrolled: ", "scroll up!");
fab2.setVisibility(View.VISIBLE);
}
else
Log.i("RecyclerView scrolled: ", "scroll down!");
fab2.setVisibility(View.GONE);
/* Log.d("hiii", "scroll hua hai");
RecyclerView.Recycler recycler = null;
Log.d("scroll", "dx: " + dx + " dy: " + dy);
Integer visibleItemCount = mRecyclerView.getChildCount();
Integer totalItemCount = mLayoutManager.getItemCount();
Integer firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
// Integer firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
Log.d("scroll", "visibleitemcount " + visibleItemCount);
Log.d("scroll", "totalitemcount " + totalItemCount);
Log.d("scroll", "firstvisibleitem " + firstVisibleItem);
final int lastItem = firstVisibleItem + visibleItemCount;
Log.d("scroll", "lastitem " + lastItem);
Log.d("hii", "lastitem " + lastItem);
Log.d("smooth", "firstvis : " + firstVis + " lastItem : " + lastItem);
if (page == 0 && visibleItemCount == totalItemCount) {
Log.d("hii", "did nothing ");
} else {
if (stay_on_this_page == false) {
Log.d("hii", "ItemCountLast");
Log.d("hii", "" + preLast + "" + lastItem);
if (preLast != lastItem) { //to avoid multiple calls for last item
if (lastItem >= totalItemCount - 5) {
preLast = lastItem;
// preLast = firstVisibleItem;
firstVis = firstVisibleItem + 1;
Log.d("llll", "preLast :" + preLast);
page++;
stay_on_this_page = true;
Momoe_Search_requestData();
} else {
Log.d("hii", "Last");
}
} else {
Log.d("smooth_scroll", "khali");
}
}
}*/
}
});
return view;
}
答案 0 :(得分:0)
您可以直接从Activity调用片段中的方法。此方法将返回将循环视图滚动到零位置。 Activity将调用顶部片段实例进行滚动。
currentFragment.scrollToTop();
片段将要求滚动recyclerView
scrollToTop()
{
mRecyclerView.smoothScrollToPosition(0);
}