【问题标题】:Android- layer beneath the cardviews when scrolling in recyclerview在recyclerview中滚动时,cardviews下面的Android层
【发布时间】:2017-08-14 16:40:59
【问题描述】:

对于一个个人项目,我正在构建一个应用程序,它显示欧洲最受欢迎的足球(或者我应该说是足球?)联赛的当前表格。该表是一个recyclerview,里面的每个团队都是一个cardview。一开始我的设备处于纵向模式时,一切正常,但是当我切换到横向模式然后又回到纵向时问题就开始了。在那之后,我在我当前的卡片视图下面看到了另一层卡片视图,一切都变得非常丑陋。

This is how it looks when everything gets messy

我的 recyclerview 适配器:

public class TeamAdapter extends RecyclerView.Adapter<TeamAdapter.TeamViewHolder> {

private TeamLeagueStandings[] teams;
public TeamAdapter(TeamLeagueStandings[] teams){
    this.teams=teams;
}

@Override
public TeamAdapter.TeamViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.
            from(parent.getContext()).
            inflate(R.layout.card_view_team, parent, false);

    return new TeamViewHolder(itemView);
}

@Override
public void onBindViewHolder(TeamAdapter.TeamViewHolder holder, int position) {
    TeamLeagueStandings team = teams[position];
    holder.teamname.setText(team.getTeamName());
    holder.matches.setText(Integer.toString(team.getCurGames()));
    holder.wins.setText(Integer.toString(team.getWins()));
    holder.draws.setText(Integer.toString(team.getDraws()));
    holder.losses.setText(Integer.toString(team.getLosses()));
    holder.gd.setText(Integer.toString(team.getGoalDifference()));
    holder.points.setText(Integer.toString(team.getPoints()));

}

@Override
public int getItemCount() {
    return teams.length;
}}

我的视图持有者(适配器内部的内部类):

public static class TeamViewHolder extends RecyclerView.ViewHolder{

        protected TextView teamname;
        protected TextView matches;
        protected TextView wins;
        protected TextView draws;
        protected TextView losses;
        protected TextView gd;
        protected TextView points;

        public TeamViewHolder(View itemView) {

            super(itemView);
            teamname=itemView.findViewById(R.id.teamName_txtview);
            matches=itemView.findViewById(R.id.matches_txtview);
            wins=itemView.findViewById(R.id.wins_txtview);
            draws=itemView.findViewById(R.id.draws_txtview);
            losses=itemView.findViewById(R.id.losses_txtview);
            gd=itemView.findViewById(R.id.gd_txtview);
            points=itemView.findViewById(R.id.points_txtview);


        }

    }

最后,我的主要片段,我声明了recyclerview:

public class TableStandingsFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    TeamAdapter adapter;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TableStandingsFragment newInstance(String param1, String param2) {
        TableStandingsFragment fragment = new TableStandingsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    public TeamLeagueStandings [] GetPLTeams() throws IOException, JSONException {
        URL urlPL= League_standings.GetPLQuery();
        TeamLeagueStandings[] teams=League_standings.LeagueStandingsArray(urlPL);
        return  teams;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_table, container, false);

        try {
            adapter= new TeamAdapter(GetPLTeams());
            RecyclerView recyclerView =  (RecyclerView) v.findViewById(R.id.recyler_teams);
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(adapter);
            recyclerView.addItemDecoration(new VerticalSpaceItemDecorator(30));
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(layoutManager);


        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return v;
    }


    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
    class VerticalSpaceItemDecorator extends RecyclerView.ItemDecoration {

        private final int spacer;

        public VerticalSpaceItemDecorator(int spacer) {
            this.spacer = spacer;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.bottom = spacer;
        }
    }
    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

当然,还有更多代码我没有上传,我认为我上传的这段代码足以解决这个问题。 谢谢!!

【问题讨论】:

  • 发布您的 R.layout.card_view_team

标签: android


【解决方案1】:

在您的活动中尝试使用此代码进行片段交易

 getSupportFragmentManager().beginTransaction()
                    .replace(your container id, TableStandingsFragment.newInstance(param1,param2), TableStandingsFragment.class.getSimpleName())
                    .commitAllowingStateLoss();

确保您在 TableStandingsFragment 中导入正确的 Fragment

import android.support.v4.app.Fragment;

【讨论】:

  • 谢谢老哥,解决了我的问题!我认为我的错误是我只是在彼此上添加了越来越多的片段,这导致了混乱。
【解决方案2】:

从您的代码中删除recyclerView.setHasFixedSize(true);并将您的cardView高度设置为wrap_content。你必须在设置适配器之前配置recyclerview。

你的回收站视图初始化应该是这样的。

RecyclerView recyclerView =  (RecyclerView) v.findViewById(R.id.recyler_teams);
            recyclerView.addItemDecoration(new VerticalSpaceItemDecorator(30));
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerView.setAdapter(adapter);

如果您想了解有关 hasFixedSize(true) 的更多信息,请查看此处。 https://stackoverflow.com/a/40707099/5558150

https://stackoverflow.com/a/33365341/5558150

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    相关资源
    最近更新 更多