可扩展的Recyclerview自定义子实现问题

时间:2016-08-30 06:32:11

标签: android android-recyclerview expandablerecyclerview

我正在研究网球应用程序,我想在这里展示现场比分锦标赛(Expandable Recyclerview),我在这里添加了一个屏幕截图

Wanna to achive like this

我正在使用此lib进行可扩展的recyclerview HERE我不知道如何在expandable中添加自定义子数据和父数据。

我复制了这个样本,但我不知道该怎么做。我解析了这个json见HERE

      if (responseString != null) {

                try {
                    String tournament = null, home_player = null, home_player_point = null, away_player = null, away_player_point = null,HomeOne=null,AwayOne=null,HomeTwo=null,AwayTwo=null,HomeThree=null,AwayThree=null,HomeFour=null,AwayFour=null,HomeFive=null,AwayFive=null;

                    JSONObject MainObject = new JSONObject(responseString).getJSONObject("list").getJSONObject("Sport").getJSONObject("2").getJSONObject("Matchday").getJSONObject("2016-08-20").getJSONObject("Match");

//                    Log.d("", "onPostExecute: jObject = " + MainObject);

                    Iterator<String> keys = MainObject.keys();
                    while (keys.hasNext()) {
                        String MatchNoKey = keys.next();
                        Log.v("list MatchNoKey = ", MatchNoKey);

                        JSONObject MatchNoObj;

                        if(MainObject.get(MatchNoKey) instanceof JSONObject) {

                            MatchNoObj = MainObject.getJSONObject(MatchNoKey);
                            String id = MatchNoObj.getString("id");
                            String leagueType = MatchNoObj.getString("leagueType");
                            String startTime = MatchNoObj.getString("startTime");
                            String status = MatchNoObj.getString("status");
                            String leagueCode = MatchNoObj.getString("leagueCode");
                            Log.v("details", "id = " + id + ", " + "leagueType = " + leagueType + ", " + "startTime = " + startTime + ", " + "status = " + status + ", " + "leagueCode = " + leagueCode );
//                            after complete matchno object info
//                            now we get the home Object
                            if(MatchNoObj.has("Home")) {

                                JSONObject HomeObj = MatchNoObj.getJSONObject("Home");

                                Log.d(TAG, "onPostExecute: Home pass");

                                String id_home = HomeObj.getString("id");
                                home_player = HomeObj.getString("name");
                                String serve = HomeObj.getString("serve");

                                Log.v("details", "id_home = " + id_home + ", " + "player0ne = " + home_player + ", " + "serve = " + serve );

                            }

//                            Away Object
                            if(MatchNoObj.has("Away")){

                                JSONObject AwayObj = MatchNoObj.getJSONObject("Away");

                                Log.d(TAG, "onPostExecute: Away pass");

                                String id_away = AwayObj.getString("id");
                                away_player = AwayObj.getString("name");
                                String serve = AwayObj.getString("serve");

                                Log.v("details", "id_away = " + id_away + ", " + "playerTwo = " + away_player + ", " + "serve = " + serve );

                            }


//                            Result object
                            if(MatchNoObj.has("Results")){

                                JSONObject ResultObj = MatchNoObj.getJSONObject("Results");

                                Log.d(TAG, "onPostExecute: ResultObj pass");

                                if (ResultObj.has("1")){

                                    JSONObject leagueObj = ResultObj.getJSONObject("league");
                                    String CurrentScore = leagueObj.getString("value");
                                    String[] score = CurrentScore.split("-");

                                    home_player_point = score[0];
                                    away_player_point = score[1];
                                }

                                if (ResultObj.has("Period")){

                                    JSONObject PeriodObj = ResultObj.getJSONObject("league");
                                    Iterator<String> Periodkeys = PeriodObj.keys();

                                    while (Periodkeys.hasNext()) {
                                        String innerkey = Periodkeys.next();
                                        Log.v("list key", innerkey);

                                        if(PeriodObj.get(innerkey) instanceof JSONObject) {

                                            JSONObject innerJObject = PeriodObj.getJSONObject(innerkey);
                                            String current_score = innerJObject.getString("value");
                                            String tiebreak = innerJObject.getString("tiebreak");

                                            Log.v("details ", "current_score = " + current_score + ", " + "tiebreak = " + tiebreak );

                                        } else if (PeriodObj.get(innerkey) instanceof String){
                                            String value = PeriodObj.getString("type");
                                            Log.v("key = type", "value = " + value);
                                        }

                                    }
                                }

                                Log.v("details information", "home_player_point = "+home_player_point+ ", " + "away_player_point = " + away_player_point);

                            }

//                            Information Object
                            if(MatchNoObj.has("Information")){

                                JSONObject InfoObj = MatchNoObj.getJSONObject("Information");

                                Log.d(TAG, "onPostExecute: InfoObj pass");

                                String shortName = InfoObj.getString("shortName");
                                String round = InfoObj.getString("round");
                                String note = InfoObj.getString("note");
                                String leagueName=null;

                                if (InfoObj.has("league")){

                                    JSONObject leagueObj = InfoObj.getJSONObject("league");
                                    tournament = leagueObj.getString("name")+" "+shortName;
                                }

                                Log.v("details information", "tournament = "+tournament+ ", " + "round = " + round + ", " + "note = " + note );

                            }


                        } else if (MainObject.get(MatchNoKey) instanceof String){
                            MatchNoObj = MainObject.getJSONObject(MatchNoKey);
                            String value = MatchNoObj.getString(MatchNoKey);
                            Log.v("key = type", "value = " + value);
                        }


                    }

                    progressBar.setVisibility(View.INVISIBLE);

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


            } else {
                progressBar.setVisibility(View.INVISIBLE);
                Toast.makeText(getApplicationContext(), "Couldn't get Data from server", Toast.LENGTH_SHORT).show();
            }

        }

这是我的SAMPLE

我不知道如何在其中添加子数据。因为我在孩子身上有多个数据。

Ingredient beef = new Ingredient("beef");
    Ingredient cheese = new Ingredient("cheese");
    Ingredient salsa = new Ingredient("salsa");
    Ingredient tortilla = new Ingredient("tortilla");
    Ingredient ketchup = new Ingredient("ketchup");
    Ingredient bun = new Ingredient("bun");

    Recipe taco = new Recipe("taco", Arrays.asList(beef, cheese, salsa, tortilla));
    Recipe quesadilla = new Recipe("quesadilla", Arrays.asList(cheese, tortilla));
    Recipe burger = new Recipe("burger", Arrays.asList(beef, cheese, ketchup, bun));
    final List<Recipe> recipes = Arrays.asList(taco, quesadilla, burger);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    mAdapter = new RecipeAdapter(this, recipes);
    mAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
        @Override
        public void onListItemExpanded(int position) {
            Recipe expandedRecipe = recipes.get(position);

            String toastMsg = getResources().getString(R.string.expanded, expandedRecipe.getName());
            Toast.makeText(MainActivity.this,
                    toastMsg,
                    Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        public void onListItemCollapsed(int position) {
            Recipe collapsedRecipe = recipes.get(position);

            String toastMsg = getResources().getString(R.string.collapsed, collapsedRecipe.getName());
            Toast.makeText(MainActivity.this,
                    toastMsg,
                    Toast.LENGTH_SHORT)
                    .show();
        }
    });

    recyclerView.setAdapter(mAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    new GetLiveData().execute();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mAdapter.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mAdapter.onRestoreInstanceState(savedInstanceState);
}

这是我的POJO

公共课程LiveScore {

String tournament = " ", home_player = " ", home_player_point = " ", away_player = " ", away_player_point = " ", home_Round1 = " ", home_Round2 = " ", home_Round3 = " ", home_Round4 = " ", home_Round5 = " ",away_Round1 = " ", away_Round2 = " ", away_Round3 = " ", away_Round4 = " ", away_Round5 = " ", League = " ", Status = " ";


public LiveScore(String tournament, String home_player, String home_player_point, String away_player, String away_player_point, String home_Round1, String home_Round2, String home_Round3, String home_Round4, String home_Round5, String away_Round1, String away_Round2, String away_Round3, String away_Round4, String away_Round5, String league, String Status) {
    this.tournament = tournament;
    this.home_player = home_player;
    this.home_player_point = home_player_point;
    this.away_player = away_player;
    this.away_player_point = away_player_point;
    this.home_Round1 = home_Round1;
    this.home_Round2 = home_Round2;
    this.home_Round3 = home_Round3;
    this.home_Round4 = home_Round4;
    this.home_Round5 = home_Round5;
    this.away_Round1 = away_Round1;
    this.away_Round2 = away_Round2;
    this.away_Round3 = away_Round3;
    this.away_Round4 = away_Round4;
    this.away_Round5 = away_Round5;
    this.League = league;
    this.Status = Status;
}


public String getStatus() {
    return Status;
}

public void setStatus(String status) {
    Status = status;
}

public String getTournament() {
    return tournament;
}

public void setTournament(String tournament) {
    this.tournament = tournament;
}

public String getHome_player() {
    return home_player;
}

public void setHome_player(String home_player) {
    this.home_player = home_player;
}

public String getHome_player_point() {
    return home_player_point;
}

public void setHome_player_point(String home_player_point) {
    this.home_player_point = home_player_point;
}

public String getAway_player() {
    return away_player;
}

public void setAway_player(String away_player) {
    this.away_player = away_player;
}

public String getAway_player_point() {
    return away_player_point;
}

public void setAway_player_point(String away_player_point) {
    this.away_player_point = away_player_point;
}

public String getHome_Round1() {
    return home_Round1;
}

public void setHome_Round1(String home_Round1) {
    this.home_Round1 = home_Round1;
}

public String getHome_Round2() {
    return home_Round2;
}

public void setHome_Round2(String home_Round2) {
    this.home_Round2 = home_Round2;
}

public String getHome_Round3() {
    return home_Round3;
}

public void setHome_Round3(String home_Round3) {
    this.home_Round3 = home_Round3;
}

public String getHome_Round4() {
    return home_Round4;
}

public void setHome_Round4(String home_Round4) {
    this.home_Round4 = home_Round4;
}

public String getHome_Round5() {
    return home_Round5;
}

public void setHome_Round5(String home_Round5) {
    this.home_Round5 = home_Round5;
}

public String getAway_Round1() {
    return away_Round1;
}

public void setAway_Round1(String away_Round1) {
    this.away_Round1 = away_Round1;
}

public String getAway_Round2() {
    return away_Round2;
}

public void setAway_Round2(String away_Round2) {
    this.away_Round2 = away_Round2;
}

public String getAway_Round3() {
    return away_Round3;
}

public void setAway_Round3(String away_Round3) {
    this.away_Round3 = away_Round3;
}

public String getAway_Round4() {
    return away_Round4;
}

public void setAway_Round4(String away_Round4) {
    this.away_Round4 = away_Round4;
}

public String getAway_Round5() {
    return away_Round5;
}

public void setAway_Round5(String away_Round5) {
    this.away_Round5 = away_Round5;
}

public String getLeague() {
    return League;
}

public void setLeague(String league) {
    League = league;
}

}

1 个答案:

答案 0 :(得分:1)

You can achieve it with this library, there is a full example here.

Basically you group your items into sections:

class MySection extends StatelessSection {

    String header;
    List<MatchNoObj> list;
    boolean expanded = true;

    public MySection(String header, List<MatchNoObj> list) {
        // call constructor with layout resources for this Section header and items 
        super(R.layout.section_header, R.layout.section_item);
        this.myHeader = header;
        this.myList = list;
    }

    @Override
    public int getContentItemsTotal() {
        return expanded? list.size() : 0;
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        final HeaderViewHolder headerHolder = (HeaderViewHolder) holder;

        headerHolder.tvTitle.setText(title);

        headerHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expanded = !expanded;
                headerHolder.imgArrow.setImageResource(
                        expanded ? R.drawable.ic_keyboard_arrow_up_black_18dp : R.drawable.ic_keyboard_arrow_down_black_18dp
                );
                sectionAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        MatchNoObj matchNoObj = list.get(position);
        itemHolder.tvItem.setText(matchNoObj.leagueName);
    }
}

Then create instance of your sections and set up your adapter:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Add your Sections
sectionAdapter.addSection(new MySection("Western Southern Open - Cincinnati", westernSouthernMatchList));
sectionAdapter.addSection(new MySection("US Open", usOpenMatchList));

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);