android - 如何使用具有不同文本和图像的相同布局

我目前正在使用 RecyclerView 来显示国家列表,如果您单击一个国家,它会打开该国家的 Activity 并显示标题、视频和旗帜

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

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

主要 Activity

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);
}
}

最佳答案

您可以通过将数据作为附加项传递到 Intent 中来实现。
用下面的代码替换 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");

https://stackoverflow.com/questions/33540265/

相关文章:

android - RecyclerView 不显示任何图像

Python - sys.stderr 未保存到 .txt 或 .log

sql-server - 动态透视多列

email - 更改 TFS 2015 电子邮件警报中的主题

javascript - JS 闭包和函数参数

amazon-web-services - spark-ec2 --copy-aws-credent

php - 在 IIS URL 重写后用 PHP 获取 URL 参数

python - NumPy 或 SciPy 计算加权中位数

android - 我可以预加载 Animation Drawable 吗?

python - 以 CSR/COO 格式为存储在 Pandas DataFrame 中的分类数据的