在Android java中以编程方式显示类似Facebook Messenger的用户界面

2q5ifsrm  于 2022-11-27  发布在  Android
关注(0)|答案(2)|浏览(181)

图像

--
我想在上面的图像中实现这样的东西
我必须循环通过这个json数组来获取我所有的数据;包含在我的json数组中的数据例如是

{
 'img' : http:\\.....
 'name' : XYZ
 'msg' : xyz
 'time' : abc
}

//this is where I am tring to append everything
final LinearLayout rl = (LinearLayout)main.findViewById(R.id.mainL); 

for (int i = 0; i < json.length(); i++) { 
    try {
        JSONObject c = json.getJSONObject(i);
        //here components must be created and added to view
    }catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我只是想知道你如何编程样式的视图(textview,imageview等)像在上面的图像。任何帮助将不胜感激

sh7euo9m

sh7euo9m1#

好的,您看到的图像实际上是一个ListView,提供定制视图。
这是怎么回事?
您需要对BaseAdapter类进行子类化,这个子类包含底层数据,在您的例子中,这些数据是以JSON格式从Web服务器获得的。
当调用BaseAdapter子类的getView()时,您可以扩大包含ImageViewTextView的布局,以便在屏幕上显示数据。

von4xj4u

von4xj4u2#

答案是为未来的访客,因为这个问题是很久以前提出的。首先,我会把你重定向到我的GitHub page,我用RecyclerView显示数据和Retrofit调用数据。我也用Volley。最后,让我展示一个简单的方法来加载数据到recyclerView。
您需要一个额外的类,称为CustomAdapter。

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

    Context context;
    private String countryList[];
    private int imgList[];

    //constructor
    public CustomAdapter(Context context, String countryList[],int imgList[]) {
        this.context = context;
        this.countryList = countryList;
        this.imgList=imgList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // inflate the item Layout
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
        // set the view's size, margins, paddings and layout parameters
        MyViewHolder vh = new MyViewHolder(view); // pass the view to View Holder
        return vh;
    }

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

        myHolder.txtName.setText(countryList[position]);
        myHolder.imgCountry.setImageResource(imgList[position]);
        // implement setOnClickListener event on item view.
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // display a toast with person name on item click
                Toast.makeText(context, countryList[position], Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return countryList.length;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView txtName;
        ImageView imgCountry;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            // get the reference of item view's
            txtName =  itemView.findViewById(R.id.txt_name);
            imgCountry =  itemView.findViewById(R.id.img_flag);
        }
    }
}

在MainActivity中,您已经为RecyclerView声明了一个LayoutManager,并且必须调用CustomAdapter才能在RecyclerView中加载数据。

String countryList[]={"Bangladesh","India","China","Australia","America","New Zealand","Portugal"};
    int imgList[]={R.drawable.bd,R.drawable.india,R.drawable.china,R.drawable.australia,R.drawable.america,R.drawable.new_zealand,R.drawable.portugle};

    RecyclerView recyclerView;

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

        // get the reference of RecyclerView
        recyclerView = findViewById(R.id.recycler_view);

        // set a LinearLayoutManager with default vertical orientation
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());

        // set a LinearLayoutManager with default Horizontal orientation
        // LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false);

        recyclerView.setLayoutManager(linearLayoutManager);

        //  call the constructor of CustomAdapter to send the reference and data to Adapter
        CustomAdapter customAdapter = new CustomAdapter(MainActivity.this,countryList, imgList);
        recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView

    }

活动_主要. xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img_flag"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="5dp"
            app:srcCompat="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/txt_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center|left"
            android:paddingLeft="10dp"
            android:text="TextView"
            android:textSize="24sp" />
    </LinearLayout>
</LinearLayout>

这是github repository。如果你从服务器调用数据,那么请访问我的Volley repo。

相关问题