2016-08-15 64 views
0

我仍然沒有信心使用android,我在這裏有2個問題,我想使3個級別的多級recyclerView。第一級是類別RecyclerView,第二級是ListView和練習列表,第三級是細節。所以現在我做了一級recyclerView這是不可點擊的。另一個問題是如何在第二級獲取Listview到第三級的細節?這是我的第一級段的代碼:Android Json和RecyclerView不可點擊,MultiLevel問題

public class BallTrainingFragment extends Fragment { 

View mRootView; 
String URL_TO_HIT = "https://gist.githubusercontent.com/tomasmaks/afbf3e836dabd72c95c4b3ec90e291ed/raw/7ba41948865f78f1ccb4058c61f8c6e06c601300/BasketballTraining.json"; 
BallTrainingAdapter adapter; 
RecyclerView mRecyclerView; 
private List<BallTrainingModel> ballTraining; 
GridLayoutManager mGridLayoutManager; 

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

public static BallTrainingFragment newInstance(int sectionNumber) { 
    BallTrainingFragment fragment = new BallTrainingFragment(); 
    Bundle args = new Bundle(); 
    args.putInt(Constants.ARG_SECTION_NUMBER, sectionNumber); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Create default options which will be used for every 
    // displayImage(...) call if no options will be passed to this method 
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() 
      .cacheInMemory(true) 
      .cacheOnDisk(true) 
      .build(); 
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity().getApplicationContext()) 
      .defaultDisplayImageOptions(defaultOptions) 
      .build(); 
    ImageLoader.getInstance().init(config); // Do it on Application start 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 


    // Inflate the layout for this fragment 
    mRootView = inflater.inflate(R.layout.fragment_balltraining, container, false); 

    mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.balltraining_list); 

    mRecyclerView.setHasFixedSize(true); 

    // Display under one column 
    mGridLayoutManager = new GridLayoutManager(getActivity(), 2); 
    mRecyclerView.setLayoutManager(mGridLayoutManager); 

    // Set orientation 
    mGridLayoutManager.setOrientation(GridLayoutManager.VERTICAL); 
    mRecyclerView.setLayoutManager(mGridLayoutManager); 

    return mRootView; 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    new FetchBallTask().execute(URL_TO_HIT); 
} 

public class FetchBallTask extends AsyncTask<String, String, List<BallTrainingModel>> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 


    @Override 
    protected List<BallTrainingModel> doInBackground(String... params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuilder buffer = new StringBuilder(); 
      String line =""; 
      while ((line = reader.readLine()) != null){ 
       buffer.append(line); 
      } 

      String finalJson = buffer.toString(); 

      JSONObject parentObject = new JSONObject(finalJson); 
      JSONArray parentArray = parentObject.optJSONArray("BasketballTraining"); 

      List<BallTrainingModel> ballTrainingModelList = new ArrayList<>(); 

      Gson gson = new Gson(); 
      for(int i=0; i<parentArray.length(); i++) { 
       JSONObject finalObject = parentArray.getJSONObject(i); 
       /** 
       * below single line of code from Gson saves you from writing the json parsing yourself which is commented below 
       */ 
       BallTrainingModel ballTrainingModel = gson.fromJson(finalObject.toString(), BallTrainingModel.class); 
       ballTrainingModel.setCategory(finalObject.getString("category")); 
       ballTrainingModel.setThumbnail(finalObject.getString("thumb")); 

       List<BallTrainingModel.Cast> castList = new ArrayList<>(); 
       for(int j=0; j<finalObject.getJSONArray("cast").length(); j++){ 
        BallTrainingModel.Cast cast = new BallTrainingModel.Cast(); 
        cast.setName(finalObject.getJSONArray("cast").getJSONObject(j).getString("name")); 
        castList.add(cast); 
       } 
       ballTrainingModel.setCastList(castList); 
       // adding the final object in the list 
       ballTrainingModelList.add(ballTrainingModel); 
      } 

      return ballTrainingModelList; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if(connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if(reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 


    @Override 
    protected void onPostExecute(final List<BallTrainingModel> result) { 
     super.onPostExecute(result); 

     if(result != null) { 
      adapter = new BallTrainingAdapter(getActivity().getApplicationContext(), R.layout.fragment_balltraining_content, result); 
      mRecyclerView.setAdapter(adapter); 
      mRecyclerView.setOnClickListener(new AdapterView.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        int position = mRecyclerView.indexOfChild(v); 

        BallTrainingModel ballTrainingModel = result.get(position); 
        Intent intent = new Intent(getActivity(), BallTrainingDetailsActivity.class); 
        intent.putExtra("ballTrainingModel", new Gson().toJson(ballTrainingModel)); 
        getActivity().startActivity(intent); 
       } 
      }); 
     } else { 
      Toast.makeText(getActivity().getApplicationContext(), "Not able to fetch data from server, please check url.", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

public class BallTrainingAdapter extends RecyclerView.Adapter<CustomViewHolder> { 
    private List<BallTrainingModel> ballTrainingList; 
    private int resource; 
    private LayoutInflater inflater; 

    public BallTrainingAdapter(Context context,int resource, List<BallTrainingModel> objects) { 
     ballTrainingList = objects; 
     this.resource = resource; 
     inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) { 
     View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_balltraining_content, null); 

     CustomViewHolder viewHolder = new CustomViewHolder(view); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(CustomViewHolder customViewHolder, int position) { 
     BallTrainingModel ballTraining = ballTrainingList.get(position); 
    ImageLoader.getInstance().displayImage(ballTrainingList.get(position).getThumbnail(), customViewHolder.thumbnail); 

     //Setting text view title 
     customViewHolder.textView.setText(ballTrainingList.get(position).getCategory()); 
    } 

    @Override 
    public int getItemCount() { 
     return (null != ballTrainingList ? ballTrainingList.size() : 0); 
    } 

} 

public class CustomViewHolder extends RecyclerView.ViewHolder{ 

    protected TextView textView; 
    protected ImageView thumbnail; 

    public CustomViewHolder(View view) { 
     super(view); 
     this.textView = (TextView) view.findViewById(R.id.category); 
     this.thumbnail = (ImageView) view.findViewById(R.id.thumbnail); 
    } 
} 
} 

我認爲不可點擊的問題是在這行代碼:

mRecyclerView.setOnClickListener(new AdapterView.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

這裏是第二級片段,這應該呈現的ListView並可點擊到第三級細節。有人可以舉例說明如何做到這一點?

public class BallTrainingDetailsActivity extends ActionBarActivity { 

private TextView name; 
private TextView tvCast; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_balltraining_list); 

    // Showing and Enabling clicks on the Home/Up button 
    if(getSupportActionBar() != null) { 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
    } 

    // setting up text views and stuff 
    setUpUIViews(); 

    // recovering data from MainActivity, sent via intent 
    Bundle bundle = getIntent().getExtras(); 
    if(bundle != null){ 
     String json = bundle.getString("ballTrainingModel"); 
     BallTrainingModel ballTrainingModel = new Gson().fromJson(json, BallTrainingModel.class); 


     StringBuffer stringBuffer = new StringBuffer(); 
     for(BallTrainingModel.Cast cast : ballTrainingModel.getCastList()){ 
      stringBuffer.append(cast.getName() + ", "); 
     } 

     tvCast.setText(stringBuffer); 
     //tvStory.setText(movieModel.getStory()); 

    } 

} 

private void setUpUIViews() { 
    name = (TextView)findViewById(R.id.Name); 
} 
} 

這裏是JSON文件:

{ 
"BasketballTraining": [ 
{ 
    "thumb":"https://kycapitalliving.files.wordpress.com/2014/02/basketball-thumbnail.jpg", 
    "category": "Ball Handling", 
    "cast": [ 
    { 
     "name": "Robert Downey Jr." 
    }, 
    { 
     "name": "Chris Evans" 
    }, 
    { 
     "name": "Mark Ruffalo" 
    } 
    ] 
}, 
{ 
    "thumb":"https://kycapitalliving.files.wordpress.com/2014/02/basketball-thumbnail.jpg", 
    "category": "Shooting", 
    "cast": [ 
    { 
     "name": "Matthew McConaughey" 
    }, 
    { 
     "name": "Anne Hathaway" 
    }, 
    { 
     "name": "Jessica Chastain" 
    }, 
    { 
     "name": "Wes Bentley" 
    } 
    ] 
}, 
{ 
    "thumb":"https://kycapitalliving.files.wordpress.com/2014/02/basketball-thumbnail.jpg", 
    "category": "Post Moves", 
    "cast": [ 
    { 
     "name": "Miles Teller" 
    }, 
    { 
     "name": "Kate Mara" 
    }, 
    { 
     "name": "Michael B. Jordan" 
    } 
    ] 
}, 
{ 
"thumb":"https://kycapitalliving.files.wordpress.com/2014/02/basketball-thumbnail.jpg", 
"category": "Defense", 
    "cast": [ 
    { 
     "name": "Christian Bale" 
    }, 
    { 
     "name": "Heath Ledger" 
    }, 
    { 
     "name": "Aaron Eckhart" 
    } 
    ] 
}, 
{ 
    "thumb":"https://kycapitalliving.files.wordpress.com/2014/02/basketball-thumbnail.jpg", 
    "category": "Scoring", 
    "cast": [ 
    { 
     "name": "Viggo Mortensen" 
    }, 
    { 
     "name": "Ian McKellen" 
    }, 
    { 
     "name": "Elijah Wood" 
    } 
    ] 
    }, 
{ 
    "thumb":"https://kycapitalliving.files.wordpress.com/2014/02/basketball-thumbnail.jpg", 
    "category": "Others", 
    "cast": [ 
    { 
     "name": "Roberto Benigni" 
    }, 
    { 
     "name": "Nicoletta Braschi" 
    }, 
    { 
     "name": "Giorgio Cantarini" 
    } 
    ] 
} 
] 
} 

,這裏是我的模型:

public class BallTrainingModel { 
private String thumbnail; 
private String category; 
@SerializedName("cast") 
private List<Cast> castList; 

public String getCategory() { 
    return category; 
} 

public void setCategory(String category) { 
    this.category = category; 
} 

public List<Cast> getCastList() { 
    return castList; 
} 

public String getThumbnail() { 
    return thumbnail; 
} 

public void setThumbnail(String thumbnail) { 
    this.thumbnail = thumbnail; 
} 

public void setCastList(List<Cast> castList) { 
    this.castList = castList; 
} 

public static class Cast { 
    private String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 
} 

任何實例或幫助將不勝感激。先謝謝你。

回答

0

RecyclerView適配器有一個方法onBindView
你必須申請有setOnClickListener:

例如:

@Override 
public void onBindViewHolder(MyViewHolder holder, final int position) { 

holder.mCardView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
       //Your Code 
     }); 
    } 
} 

對於裁判:檢查這link