如何使用不同文本和图像的相同布局

时间:2015-11-05 09:01:08

标签: android android-recyclerview

如果您点击其打开该国家/地区活动的国家/地区并显示标题,视频和标记

,我正在使用RecyclerView来显示目前的国家/地区列表

而不是制作200多个活动,并在每个活动中放置标题,视频和旗帜。是否可以将一个活动与所有标题,视频和图像一起使用,并制作切换语句或其他内容,以便使用相同的布局文件在所有这些活动之间切换。

因此,如果用户点击国家/地区,则每次都会打开相同的活动,但只需切换并显示该国家/地区的正确标题,视频和图片。

主要活动

public class MainActivity extends ActionBarActivity {


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

    initControls();
}

private void initControls() {

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Countries");

    }

    final String[] versions = {

            "Afghanistan",
            "Albania",

            .....
    };


    ArrayList<Properties> countries = new ArrayList<Properties>();

    for (String version : versions) {
        Properties feed = new Properties();

        feed.setTitle(version);
        countries.add(feed);
    }

    recyclerView.setHasFixedSize(true);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    RecyclerView.Adapter mAdapter = new CardViewDataAdapter(countries);

    recyclerView.setAdapter(mAdapter);


}
}

适配器

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

private static ArrayList<Properties> dataSet;

public CardViewDataAdapter(ArrayList<Properties> countries) {

    dataSet = countries;
}


@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
            R.layout.card_view, null);

    return new ViewHolder(itemLayoutView);
}

@Override
public void onBindViewHolder(CardViewDataAdapter.ViewHolder viewHolder, int i) {

    Properties fp = dataSet.get(i);

    viewHolder.countryName.setText(fp.getTitle());
    viewHolder.feed = fp;
}

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


public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView countryName;

    public Properties feed;
    private final Context context;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        context = itemView.getContext();

        countryName = (TextView) itemLayoutView
                .findViewById(R.id.cName);

        itemLayoutView.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                final Intent intent;
                switch (getAdapterPosition()){
                    case 0:
                        intent =  new Intent(context, Afghanistan.class);
                        break;

                    case 1:
                        intent =  new Intent(context, Albania.class);
                        break;


                    .......

                }
                context.startActivity(intent);
            }
        });


    }

}
}

国家/地区布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/countryTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_marginTop="40dp"
    android:layout_marginBottom="40dp"
    android:layout_gravity="center_horizontal" />

<VideoView
    android:id="@+id/countryVideo"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_marginBottom="40dp"
    android:layout_gravity="center_horizontal"/>

<ImageView
    android:id="@+id/countryFlag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" />


</LinearLayout>

阿富汗

public class Afganistan extends Activity {

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

    TextView countryTitle = (TextView)findViewById(R.id.countryTitle);
    countryTitle.setText("Afganistan");

    VideoView view = (VideoView)findViewById(R.id.countryVideo);
    String path = "android.resource://" + getPackageName() + "/" + R.raw.afganistanVideo;
    view.setVideoURI(Uri.parse(path));
    view.start();
    view.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setLooping(true);
        }
    });

    ImageView countryFlag = (ImageView)findViewById(R.id.countryFlag);
    countryFlag.setImageResource(R.drawable.afganistanFlag);
}
}

1 个答案:

答案 0 :(得分:0)

您可以通过传递意图中的数据作为附加内容来实现 使用下面的代码替换onClick()方法中的代码,

        Intent countryIntent = new Intent(context, CountryActivity.class);
        countryIntent.putExtra("title", versions[getAdapterPosition()]);
        countryIntent.putExtra("url", /*set the url as specified above*/]);
        context.startActivity(countryIntent);

并在Afganistan Activity的onCreate()方法中使用以下代码

        Intent countryIntent = getIntent();
        countryTitle.setText(countryIntent.getStringExtra("title"));
        String path = "android.resource://" + getPackageName() + "/" + countryIntent
                .getStringExtra("url");