Android 当我在asynctask中调用notifydatasetchanged时,我的Recyclerview滚动到顶部
当我在asynctask中调用notifydatasetchanged时,我的Recyclerview滚动到顶部 如何停止滚动到顶部,这是LoadrsFeedSitems异步任务中的问题 我的回收视图Android 当我在asynctask中调用notifydatasetchanged时,我的Recyclerview滚动到顶部,android,android-asynctask,android-recyclerview,Android,Android Asynctask,Android Recyclerview,当我在asynctask中调用notifydatasetchanged时,我的Recyclerview滚动到顶部 如何停止滚动到顶部,这是LoadrsFeedSitems异步任务中的问题 我的回收视图 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG_LIFE, "In
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG_LIFE, "Inside onCreat");
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG_LIFE, "Inside onViewCreated");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.head_line_fragment, container, false);
Log.i(TAG_LIFE, "Inside onCreateView");
view = rootView;
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(getActivity());
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
} else {
mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
}
if (savedInstanceState != null) {
mDataset = savedInstanceState.getParcelableArrayList(Constants.MY_DATA_SET_KEY);
mAdapter = new RecyclerAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
}
setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
return rootView;
}
public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
int scrollPosition = 0;
if (mRecyclerView.getLayoutManager() != null) {
scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
.findFirstCompletelyVisibleItemPosition();
}
switch (layoutManagerType) {
case GRID_LAYOUT_MANAGER:
mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
break;
case LINEAR_LAYOUT_MANAGER:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
break;
default:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
}
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.scrollToPosition(scrollPosition);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
savedInstanceState.putParcelableArrayList(Constants.MY_DATA_SET_KEY, mDataset);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && mDataset.isEmpty()) {
Log.i(TAG_LIFE, "setUserVisibleHint");
new LoadRssFeedsItems().execute(Constants.HEALTH);
}
this.isVisibleToUser=isVisibleToUser;
Log.i(TAG_LIFE,"setUserVisibleHint"+String.valueOf(this.isVisibleToUser));
}
private enum LayoutManagerType {
GRID_LAYOUT_MANAGER,
LINEAR_LAYOUT_MANAGER
}
public class LoadRssFeedsItems extends AsyncTask<String, Void, String> {
int i = 0;
private final String Not_interasted_catguty_in_Cutture = mySharedPreferences.loadStringPrefs(Constants.REMOVE_KEY_CULTURE_CATEGORY, "N/A", MainActivity.context);
private final String Not_interasted_catguty_in_Since = mySharedPreferences.loadStringPrefs(Constants.REMOVE_KEY_SINCE_CATEGORY, "N/A", MainActivity.context);
private final String Not_interasted_catguty_in_Sport= mySharedPreferences.loadStringPrefs(Constants.REMOVE_KEY_SPORT_CATEGORY, "N/A", MainActivity.context);
private final String Not_interasted_catguty_in_Socity= mySharedPreferences.loadStringPrefs(Constants.REMOVE_KEY_SOCITY_CATEGORY, "N/A", MainActivity.context);
public LoadRssFeedsItems() {
}
protected String doInBackground(String... urls) {
try {
Document rssDocument = Jsoup.connect(urls[0]).timeout(Constants.BIG_TIME_OUT).ignoreContentType(true).parser(Parser.xmlParser()).get();
Elements mItems = rssDocument.select(Constants.ITEM);
RssItem rssItem;
for (Element element : mItems) {
mTitle = element.select("title").first().text();
mDescription = element.select("description").first().text();
mLink = element.select("link").first().text();
mPubDate = element.select("pubDate").first().text();
mCatgury = element.select("category").first().text();
Log.i(TAG, "Item title: " + (mTitle == null ? "N/A" : mTitle));
Log.i(TAG, "Item Description: " + (mDescription == null ? "N/A" : mDescription));
Log.i(TAG, "Item link: " + (mLink == null ? "N/A" : mLink));
Log.i(TAG, "Item data: " + (mPubDate == null ? "N/A" : mPubDate));
Log.i(TAG, "Item image link: " + (mImageURL == null ? "N/A" : mImageURL));
Log.i(TAG,mCatgury);
if(mCatgury.equals("مہاجرین کا بحران"))
{
continue;
}
rssItem = new RssItem(mTitle, mDescription, mPubDate, mLink, mCatgury, i, getActivity());
mDataset.add(rssItem);
}
} catch (Exception e) {
e.printStackTrace();
}
return "d";
}
protected void onPostExecute(String result) {
if ((CultureFlag && !mDataset.isEmpty()) || SportFlag || SinceFlag || socityFlag) {
if(CultureFlag)
{
if(mDataset.get(mDataset.size()-1).getCategory().equals("فن و ثقافت"))
{
Log.i(TAGi,"mDataset.get(mDataset.size()-1).getCategory().equals(\"فن و ثقافت\")");
CultureFlag=false;
SportFlag=true;
}
else {
Log.i(TAGi,"mDataset.get(mDataset.size()-1).getCategory().equals(\"فن و ثقافت\") else part");
}
}else if(SportFlag)
{
if(mDataset.get(mDataset.size()-1).getCategory().equals("کھیل"))
{
Log.i(TAGi,"mDataset.get(mDataset.size()-1).getCategory().equals(\"کھیل\")");
SportFlag=false;
SinceFlag=true;
}else {
}
}else if(SinceFlag)
{
if(mDataset.get(mDataset.size()-1).getCategory().equals("سائنس اور ماحول"))
{
Log.i(TAGi,"mDataset.get(mDataset.size()-1).getCategory().equals(\"سائنس اور ماحول\")");
SinceFlag=false;
socityFlag=true;
}
}else if(socityFlag)
{
if(mDataset.get(mDataset.size()-1).getCategory().equals("معاشرہ"))
{
Log.i(TAGi,"mDataset.get(mDataset.size()-1).getCategory().equals(\"معاشرہ\")");
socityFlag=false;
}
}
}
if(!mDataset.isEmpty() && mDataset.get(mDataset.size()-1).getCategory().equals("صحت"))
{
CultureFlag=true;
}
if (CultureFlag) {
Log.i(TAGi, " CultureFlag");
if (Not_interasted_catguty_in_Cutture.equals("N/A"))
{
new LoadRssFeedsItems().execute(Constants.ART_AND_CULTURE);
}
else
{
SportFlag = true;
CultureFlag=false;
}
}
if (SportFlag) {
if(Not_interasted_catguty_in_Sport.equals("N/A"))
{
Log.i(TAGi, " SportFlag");
new LoadRssFeedsItems().execute(Constants.SPORT);
}else{
SinceFlag = true;
SportFlag=false;
}
}
if (SinceFlag) {
if(Not_interasted_catguty_in_Since.equals("N/A"))
{
Log.i(TAGi, " SinceFlag");
new LoadRssFeedsItems().execute(Constants.SCIENCE_AND_ENVIMENT);
}else {
SinceFlag=false;
socityFlag=true;
}
}
if(socityFlag)
{
if(Not_interasted_catguty_in_Socity.equals("N/A"))
{
new LoadRssFeedsItems().execute(Constants.SOCIETY);
}
else {
socityFlag=false;
}
}
if (mDataset.isEmpty()) {
try {
Snackbar mSnackbar = Snackbar.make(view, "Unable to connect", Snackbar.LENGTH_INDEFINITE)
.setAction("RETRY", new View.OnClickListener() {
@Override
public void onClick(View view) {
new LoadRssFeedsItems().execute(Constants.HEALTH);
}
});
mSnackbar.show();
} catch (NullPointerException n) {
}
}else if(mDataset.isEmpty() && mDataset.get(mDataset.size()-1).getCategory().equals("صحت"))
{
mAdapter.notifyDataSetChanged();
}
else {
mAdapter = new RecyclerAdapter(mDataset);
Log.i(TAGi, "m data set size: " + mDataset.size());
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
if (!new MySharedPreferences().loadPrefs(Constants.switchStateKey, true, MainActivity.context)) {
getImageUrls get_ImageUrls = new getImageUrls();
get_ImageUrls.execute();
}
}
}
}
private class getImageUrls extends AsyncTask<Void, Void, Void> {
String mLink;
String mLastHalfUrl;
String url;
int dash_A_index;
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "Item image link in seacond thread: " + (mImageURL == null ? "N/A" : mImageURL));
try {
for (int i = 0; i < mDataset.size(); i++) {
mLink = mDataset.get(i).getPostLink();
dash_A_index = mLink.indexOf(Constants.BACK_SLASH_A);
mLastHalfUrl = mLink.substring(dash_A_index);
url = mLink.replace(Constants.BASE_URL, "");
url = url.replace(mLastHalfUrl, "");
url = StringUtils.replaceEach(URLEncoder.encode(url, "UTF-8"), new String[]{"+", "*", "%7E"}, new String[]{"%20", "%2A", "~"});
StringBuilder sb = new StringBuilder(url);
url = sb.append(mLastHalfUrl).toString();
url = Constants.BASE_URL + url;
documentImage = Jsoup.connect(url).get();
metalinks = documentImage.select(Constants.MATA_PROPTY_IMAGE);
mImageURL = metalinks.attr(Constants.CONTENT);
Log.i(TAG, "Item image link in seacond thread: " + (mImageURL == null ? "N/A" : mImageURL));
mDataset.get(i).setImageUrl(mImageURL);
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
mAdapter.notifyDataSetChanged();
}
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.i(TAG_LIFE,“内部onCreat”);
}
@凌驾
已创建视图上的公共void(视图,@Nullable Bundle savedInstanceState){
super.onViewCreated(视图,savedInstanceState);
Log.i(TAG_LIFE,“内部onViewCreated”);
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.head\u line\u碎片,容器,假);
Log.i(TAG_LIFE,“内部onCreateView”);
视图=根视图;
mRecyclerView=(RecyclerView)rootView.findviewbyd(R.id.recycler\u视图);
mLayoutManager=新的LinearLayoutManager(getActivity());
if(getResources().getConfiguration().orientation==Configuration.orientation\u纵向){
mCurrentLayoutManagerType=LayoutManagerType.LINEAR\u LAYOUT\u MANAGER;
}否则{
mCurrentLayoutManagerType=LayoutManagerType.GRID\u LAYOUT\u MANAGER;
}
如果(savedInstanceState!=null){
mDataset=savedInstanceState.getParcelableArrayList(Constants.MY_DATA_SET_KEY);
mAdapter=新的回收适配器(mDataset);
mRecyclerView.setAdapter(mAdapter);
}
SetRecycleServiceWLayoutManager(mCurrentLayoutManagerType);
返回rootView;
}
公共void setRecycleServiceWLayoutManager(LayoutManagerType LayoutManagerType){
int-scrollPosition=0;
if(mRecyclerView.getLayoutManager()!=null){
scrollPosition=((LinearLayoutManager)MRECyclView.getLayoutManager())
.findFirstCompletelyVisibleItemPosition();
}
开关(layoutManagerType){
案例网格布局管理器:
mLayoutManager=新的GridLayoutManager(getActivity(),SPAN\u COUNT);
mCurrentLayoutManagerType=LayoutManagerType.GRID\u LAYOUT\u MANAGER;
打破
案例线性布局管理器:
mLayoutManager=新的LinearLayoutManager(getActivity());
mCurrentLayoutManagerType=LayoutManagerType.LINEAR\u LAYOUT\u MANAGER;
打破
违约:
mLayoutManager=新的LinearLayoutManager(getActivity());
mCurrentLayoutManagerType=LayoutManagerType.LINEAR\u LAYOUT\u MANAGER;
}
mRecyclerView.setLayoutManager(mllayoutmanager);
mRecyclerView.scrollToPosition(滚动位置);
}
@凌驾
SaveInstanceState上的公共无效(Bundle savedInstanceState){
savedInstanceState.putSerializable(键布局管理器,mCurrentLayoutManagerType);
savedInstanceState.putParcelableArrayList(Constants.MY_DATA_SET_KEY,mDataset);
super.onSaveInstanceState(savedInstanceState);
}
@凌驾
public void setUserVisibleHint(布尔值isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser&&mDataset.isEmpty()){
Log.i(TAG_LIFE,“setUserVisibleHint”);
新LoadRssFeedsItems().execute(Constants.HEALTH);
}
this.isVisibleToUser=isVisibleToUser;
Log.i(TAG_LIFE,“setUserVisibleHint”+String.valueOf(this.isVisibleToUser));
}
私有枚举LayoutManagerType{
网格布局管理器,
线性布局管理器
}
公共类LoadRssFeedsItems扩展异步任务{
int i=0;
私有最终字符串未在\u Cutture=mySharedPreferences.loadStringPrefs(Constants.REMOVE\u KEY\u CULTURE\u catguty\u CATEGORY,“N/A”,MainActivity.context);
private final String Not_interested_catguty_in_Since=mySharedPreferences.loadStringPrefs(Constants.REMOVE_KEY_sing_Since_CATEGORY,“N/A”,MainActivity.context);
私有最终字符串未在\u Sport=mySharedPreferences.loadStringPrefs(Constants.REMOVE\u KEY\u Sport\u CATEGORY,“N/A”,MainActivity.context)中插入\u catguty\u;
私有最终字符串未在\u Socity=mySharedPreferences.loadStringPrefs(Constants.REMOVE\u KEY\u Socity\u CATEGORY,“N/A”,MainActivity.context)中插入\u catguty\u;
公共LoadrsFeedSitems(){
}
受保护的字符串doInBackground(字符串…URL){
试一试{
Document rssDocument=Jsoup.connect(URL[0]).timeout(Constants.BIG_TIME_OUT).ignoreContentType(true).parser(parser.xmlParser()).get();
Elements=rssDocument.select(Constants.ITEM);
RssItem RssItem;
用于(图元:斜接){
mTitle=element.select(“title”).first().text();
mddescription=element.select(“description”).first().text();
mLink=element.select(“链接”).first().text();
mPubDate=element.select(“pubDate”).first().text();
mCatgury=element.select(“category”).first().text();
Log.i(标记,“项目标题:”+(mTitle==null?“不适用”:mTitle));
Log.i(标记,“项目描述:”+(mDescription==null?“不适用”:mDescription));
Log.i(标记,“项目链接:”+(mLink==null?“不适用”:mLink));
Log.i(标记,“项目数据:”+(mPubDate==null?“不适用”:mPubDate));
Log.i(标记,“项目图像链接:”+(mImageURL==null?“N/A”:mImageURL));
Log.i(TAG,mCatgury);
if(mCatgury.等于
{
继续;
}
rssItem=新的rssItem(mTitle、MDDescription、mPubDate、mLink、MCATGRY、i、getActi
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fcffffff"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<ProgressBar
android:id="@+id/Progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:visibility="visible"
></ProgressBar>