Android Recycler视图在创建时滚动到某个位置

时间:2016-08-12 04:07:02

标签: android scroll android-recyclerview

嗨,我不擅长android。我有

  • ViewPager

在ViewPager中,有 RecyclerView1。,对于它的项目,

  • 的FrameLayout
    • CardView
      • 的TextView
      • RecyclerView2

每当创建View时,屏幕会自动滚动到recyclerView2中的第一个元素而不是RecyclerView1。

RecyclerView1有一些分类信息,所以我希望别人看到

创建View时,RecyclerView1中的第一个元素。

为什么会发生这件事?

添加了: 在其他活动中使用listview时会发生类似的事情。

我上面有很多卡片视图和listview

  • cardview
  • cardview
  • 列表视图

当呈现此活动时,屏幕滚动到listview的第一个项目而不是第一个cardview !!!

这是RecyclerView1的项目布局

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dp"
android:paddingRight="1dp">

    <android.support.v7.widget.CardView
        android:id="@+id/league_item_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="@dimen/leauge_card_elevation"
        card_view:cardCornerRadius="@dimen/league_card_corner_radius">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <!-- Header View -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_margin="3dp"
                >
            <ImageView
                android:id="@+id/league_logo"
                android:padding="5dp"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/champions_logo"
                />
            <TextView
                android:id="@+id/league_name"
                android:paddingLeft="4dp"
                android:layout_weight="4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Champions League"
                style="@style/league_name_text"
            />
            <TextView
                android:id="@+id/league_date"
                android:textColor="@color/primary_dark"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="07/25"
                />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/primary_dark"/>

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/league_detail_recycler_view" >
            </android.support.v7.widget.RecyclerView>
        </LinearLayout>

    </android.support.v7.widget.CardView>

</FrameLayout>

这是RecyclerView2的项目布局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/detail_layout"
    >
    <TextView
        android:id="@+id/league_team_name1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        style="@style/league_team_name_text"
        android:text="Manchester Utd"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/league_team_image1"/>

    <ImageView
        android:layout_margin="@dimen/league_team_image_margin"
        android:layout_height="50dp"
        android:layout_width="50dp"
        android:id="@+id/league_team_image1"
        android:src="@drawable/manu"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/league_score1" />

    <TextView
        android:id="@+id/league_score1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="2"
        style="@style/league_name_text"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/league_score_sperator" />
    <TextView
        android:id="@+id/league_score_sperator"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text=" : "
        style="@style/league_team_name_text"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/league_score2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="1"
        style="@style/league_name_text"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/league_score_sperator"/>

    <ImageView
        android:layout_margin="@dimen/league_team_image_margin"
        android:layout_height="50dp"
        android:layout_width="50dp"
        android:id="@+id/league_team_image2"
        android:src="@drawable/chelsea"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/league_score2" />
    <TextView

        android:id="@+id/league_team_name2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        style="@style/league_team_name_text"
        android:text="Chelsea"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/league_team_image2" />

    <TextView
        android:id="@+id/league_game_status"
        android:background="@color/accent"
        android:layout_height="60dp"
        android:gravity="center"
        android:layout_width="30dp"
        style="@style/league_name_text"
        android:text="FT"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

我认为我没有任何可关注的项目

这是填充RecyclerView1的片段

public class LeagueFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<LeagueItemModel> leagueItems;
    public static final String DATE= "date";
    public String currentDate;

    public LeagueFragment() {
        // Required empty public constructor
    }

    public static final LeagueFragment newInstance(String date)
    {
        // date as argument
        LeagueFragment f = new LeagueFragment();
        Bundle bdl = new Bundle();
        bdl.putString(DATE, date);
        f.setArguments(bdl);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.league_fragment, container, false);

        currentDate = getArguments().getString(DATE);

        // set up contents
        recyclerView = (RecyclerView)view.findViewById(R.id.league_recycler_view);
        setUpLeagueView();
        Log.d("league debug",String.valueOf(leagueItems.size()));
        return view;
    }

    private void setUpProgress(){


    }

    // Data Filling Method
    private List<LeagueItemModel> fillData(){
        // use date
        return null;
    }

    private void setUpLeagueView(){
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        manager.setAutoMeasureEnabled(true);
        //manager.scrollToPositionWithOffset(0,0);

        recyclerView.setLayoutManager(manager);

        // recyclerView Nested Scrolling behavior
        //recyclerView.setNestedScrollingEnabled(false);

        leagueItems = new ArrayList<>(30);

        initializeLeagueCardItemList();

        //setting recyclerView!
        recyclerView.setAdapter(new LeagueRecyclerAdapter(leagueItems,getActivity(),recyclerView));

        // Added!
        //recyclerView.scrollToPosition(1);

    }

    private void initializeLeagueCardItemList(){
        //
        LeagueItemModel cardItemModel;
        String[] leagueName = getResources().getStringArray(R.array.league_name);
        String[] leagueImage = getResources().getStringArray(R.array.league_image);

        final int length = leagueName.length;
        for(int i=0;i<length;i++){
            cardItemModel = new LeagueItemModel(leagueImage[i],leagueName[i],currentDate);
            leagueItems.add(cardItemModel);
        }

        //  cardItemModel = new LeagueItemModel(teamName1[i],teamName2[i],teamScore1[i],teamScore2[i],teamImage1[i],teamImage2[i]);
    }
}

这是填充RecyclerView2项目的适配器

public class LeagueRecyclerAdapter extends RecyclerView.Adapter<LeagueRecyclerAdapter.ViewHolder>{

    public Context context;
    public List<LeagueItemModel> cardItems;
    public RecyclerView parentRecyclerView;

    // Adapter Constructor
    public LeagueRecyclerAdapter(List<LeagueItemModel> cardItems, Context context, RecyclerView recyclerView){
        this.cardItems = cardItems;
        this.context=context;
        this.parentRecyclerView=recyclerView;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView leagueName, currentDate;
        ImageView leagueImage;
        RecyclerView detailRecyclerView;
        public ViewHolder(View itemView){
            super(itemView);
            this.leagueImage = (ImageView) itemView.findViewById(R.id.league_logo);
            this.leagueName = (TextView) itemView.findViewById(R.id.league_name);
            this.currentDate = (TextView) itemView.findViewById(R.id.league_date);
            this.detailRecyclerView = (RecyclerView) itemView.findViewById(R.id.league_detail_recycler_view);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.league_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // onBindViewHolder is place onItemClickListener!

        String leagueName = cardItems.get(position).leagueName;
        // Pass somekind of game identifier!
        holder.leagueName.setText(leagueName);
        holder.currentDate.setText(cardItems.get(position).day);

        String imageName = cardItems.get(position).leagueImageUrl;
        String uri = "@drawable/" + imageName;  // where myresource (without the extension) is the file

        int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
        Glide.with(context)
                .load(imageResource)
                .centerCrop()
                .placeholder(R.drawable.loading_spinner)
                .crossFade().into(holder.leagueImage);

        //Fill RecyclerView!
        setUpLeagueDetailView(holder.detailRecyclerView,leagueName);

        //parentRecyclerView.scrollToPosition(0);

    }

    @Override
    public int getItemCount() {
        return cardItems.size();
    }


    // Method for Inner League Items

    public void fillDetailData(){


    }

    //For Test
    public List<LeagueDetailItemModel> detailItems;
    private void setUpLeagueDetailView(RecyclerView recyclerView,String leagueName){
        // TODO did it with getActivity! Replace this in the collasing!
        LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        manager.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(manager);

        // This Important!!
        recyclerView.setNestedScrollingEnabled(false);


        //for test
        detailItems = new ArrayList<>(30);


        initializeDetailItemList();
        recyclerView.setAdapter(new LeagueDetailRecyclerAdapter(detailItems,context,leagueName));
    }

    //Fill data into detailItems
    private void initializeDetailItemList(){
        String[] teamName1 = getData(R.array.team_name);
        String[] teamName2 = getData(R.array.team_name);
        String[] teamImage1 = getData(R.array.team_image_name);
        String[] teamImage2 = getData(R.array.team_image_name);
        String[] teamScore1 = getData(R.array.team_score);
        String[] teamScore2 = getData(R.array.team_score);
        String[] status = getData(R.array.status);
        final int length = 10;
        LeagueDetailItemModel temp;
        for(int i=0;i<length;i++){
            temp = new LeagueDetailItemModel(teamName1[0],teamName2[1],teamScore1[0],teamScore2[1],teamImage1[0],teamImage2[1],status[0]);
            detailItems.add(temp);
        }
    }

    private String[] getData(int id){
        return context.getResources().getStringArray(id);
    }

}

1 个答案:

答案 0 :(得分:0)

如果RecyclerView2有一些可聚焦的项目,例如编辑文本,则会出现此行为。

您可以做的一件事是以编程方式将RecyclerView1位置设置为0。

从顶部linearLayoutManager.scrollToPositionWithOffset

滚动项目0到0像素