我有多个recyclerViews,它应该出现在另一个XML视图之后,

时间:2016-05-02 00:31:28

标签: java android xml android-recyclerview

我有多个recyclerViews,它们应该出现在另一个XML视图之后,但他们不会,我正在使用适配器类来管理它。使用log.v后我发现功能本身"在Adappter类"不叫,我不知道为什么??

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
    android:layout_height="match_parent"

       tools:context="com.example.linah.movielessonapp.Detailed_Movie">

<TextView
    android:id="@+id/MovieTitle"
    ... />

<ImageView
    android:id="@+id/MovieImage"
    .../>

<TextView
    android:id="@+id/MovieReview"
          ... />

<Button
    android:id="@+id/Favbutton"
                  ... />

<TextView

    android:id="@+id/Date"
          ... />

<TextView

    android:id="@+id/Rate"
          .../>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

     <android.support.v7.widget.RecyclerView
    android:id="@+id/Trailers_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/MovieReview" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/reviews_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/Trailers_recycler_view"

       />

          </LinearLayout>

         </RelativeLayout>

        </ScrollView>

公共类Detailed_Movie扩展了AppCompatActivity {

public static List<Movie_Details> movieDetailsList = new ArrayList<>();
private String ID;
public String Trailer_OR_Review = "trailer";
private boolean noConnection;
private boolean trailersDone;
private int trailersSize;
private static MoviesDetailedAdapter mAdapter;

private RecyclerView TrailerRecyclerView, ReviewsRecyclerView;





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

   TrailerRecyclerView = (RecyclerView) findViewById(R.id.Trailers_recycler_view);
    ReviewsRecyclerView = (RecyclerView) findViewById(R.id.reviews_recycler_view);


    new getData().execute("trailer");



    // adapter
    mAdapter = new MoviesDetailedAdapter(movieDetailsList,TrailerRecyclerView.getContext(),Trailer_OR_Review);
   // mAdapter = new MoviesDetailedAdapter(movieDetailsList,ReviewsRecyclerView.getContext(),Trailer_OR_Review);

    TrailerRecyclerView.setLayoutManager(new LinearLayoutManager(TrailerRecyclerView.getContext()));
   // ReviewsRecyclerView.setLayoutManager(new LinearLayoutManager(ReviewsRecyclerView.getContext()));

    TrailerRecyclerView.setItemAnimator(new DefaultItemAnimator());
  //  ReviewsRecyclerView.setItemAnimator(new DefaultItemAnimator());

    noConnection = false;
    if(isOnline(Detailed_Movie.this)) {
        new getData().execute("trailer");
         mAdapter.notifyDataSetChanged();
    }
    // set the adapter
     TrailerRecyclerView.setAdapter(mAdapter);
    prepareMovieData();




    Intent i = getIntent();
    // http://api.themoviedb.org/3/movie/{id}/videos

    String ImgPath   = "http://image.tmdb.org/t/p/w185/";
    String VideoPath = "http://www.youtube.com/watch?v=";


    String MovieTitle = i.getExtras().getString("title");
    Toast.makeText(getApplicationContext(),MovieTitle+" is selected!", Toast.LENGTH_SHORT).show();
    ImageView img    = (ImageView)findViewById(R.id.MovieImage);
    TextView  Title  = (TextView)findViewById(R.id.MovieTitle);
    TextView  Review = (TextView)findViewById(R.id.MovieReview);
    TextView  Date   = (TextView)findViewById(R.id.Date);
    TextView  Rate   = (TextView)findViewById(R.id.Rate);
    Button    Fav    = (Button)  findViewById(R.id.Favbutton);


    // get data from intent
    assert Title != null;
    Title. setText(i.getExtras().getString("title"));
    assert Review != null;
    Review.setText(i.getExtras().getString("review"));
    assert Rate != null;
    Rate.  setText(i.getExtras().getString("rate"));
    assert Date != null;
    Date.  setText(i.getExtras().getString("date"));


      ID     = i.getExtras().getString("id");
    String Imgurl = i.getExtras().getString("img");
    // append ImgPath
    switch (ImgPath = new StringBuilder()
            .append(ImgPath)
            .append(Imgurl)
            .toString()) {
    }
    // append VideoPath

    VideoPath = new StringBuilder()
            .append(VideoPath)
            .append("6uEMl2BtcqQ")
            .toString();

 //   VideoPath = VideoPath +  getString(R.string.API_KEY);
    final String finalVideoPath = VideoPath;

    if (Fav != null) {
        Fav.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(finalVideoPath));

                startActivity(intent);
            }
        });
    }

    Picasso.with(this)
            .load(ImgPath)
            .placeholder(R.drawable.loading) //this is optional the image to display while the url image is downloading
            .error(R.drawable.error)         //this is also optional if some error has occurred in downloading the image
            .into(img);


    TrailerRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), TrailerRecyclerView, new ClickListener() {
        @Override
        public void onClick(View view, int position) {
               Movie_Details movie = movieDetailsList.get(position);
            if (position < trailersSize) {  
              //  String link = ((TextView) findViewById(R.id.Link)).getText().toString();
               // String link = movie.getKey();
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + movie.getKey())));
            }
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

}

private void prepareMovieData() {


    Movie_Details movie = new Movie_Details("MovieTrailer","6uEMl2BtcqQ","Linah","verynice");
    movieDetailsList.add(movie);


    mAdapter.notifyDataSetChanged();
}
public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}

public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private MainActivity.ClickListener clickListener;

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final MainActivity.ClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                }
            }
        });
    }

    public RecyclerTouchListener(Context applicationContext, RecyclerView trailerRecyclerView, ClickListener clickListener) {
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}



public class getData extends AsyncTask<String, Void, Void> {

    ...
    }

 }

类MoviesDetailedAdapter     公共类MoviesDetailedAdapter扩展了RecyclerView.Adapter {

private List<Movie_Details> moviesList;
private Context context;
public String Trailer_OR_Review = "trailer";
public TextView TrailerName , Author , Content , TrailerLink ;


public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {



    public MyViewHolder(View view) {
        super(view);
        Log.v("here","MyViewHolder");
        TrailerName = (TextView) view.findViewById(R.id.Name);
        Author      = (TextView) view.findViewById(R.id.Author);
        TrailerLink = (TextView) view.findViewById(R.id.Link);
        Content     = (TextView) view.findViewById(R.id.Content);

        view.setOnCreateContextMenuListener(this);
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

    }
}

public MoviesDetailedAdapter(List<Movie_Details> moviesList,Context context, String trailerORReview) {
    this.moviesList = moviesList;
    this.context = context;
    Trailer_OR_Review = trailerORReview;
    Log.v("here","madapter");

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView;
    Log.v("here","onCreateViewHolder");

    itemView  = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.trailers_layout, parent, false);
    /*
    if (Trailer_OR_Review.equals("trailers")){


        itemView  = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.trailers_layout, parent, false);
    }
    else{
          itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.reviews_layout, parent, false);
    }*/
    return new MyViewHolder(itemView);
}

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

    Log.v("here","onBindViewHolder");
    Movie_Details movie_details = moviesList.get(position);
    Log.v("here",movie_details.getContent());
    Log.v("here",movie_details.getName());

    TrailerName.setText(movie_details.getName());
    TrailerLink.setText(movie_details.getKey());

    /*
    if (Trailer_OR_Review.equals("trailers")){
        TrailerName.setText(movie_details.getName());
        TrailerLink.setText(movie_details.getKey());


    }
    else{
        Author.setText(movie_details.getAuthor());
        Content.setText(movie_details.getContent());

    }*/

}

0 个答案:

没有答案