1. Achieve goals
1. Function description
Based on the last experiment, improve the content of the list items and add a click function to each item. After clicking, you can jump to another page< /strong> and can return to the original list page.
2. Realize the effect
2. Implementation instructions
There are roughly three things to do: improve the list item layout (item.xml), implement jumps, and let the page display data
java files related to it xml files related to it
Since you will reach a new page after the jump, you need to create a new activity and corresponding xml file (DetailActivity and activity_detail)
1. Complete list item layout (item.xml)
Imitate the WeChat chat interface. The left side of each item is the avatar, the upper layer on the right is the name, and the lower layer is the chat content. Just add three components.
2. Interface design after the jump (activity_detail.xml)
This time I just put the name and text content, and also added a return button, three components
3. Data display
To transfer data in a list, you need to create a data source. By using List
, you can easily store and operate multiple data items. Each data item is composed of multiple attributes. .
Store data in fragment1.java and use for loop to do it.
String[] names={"ID:a","ID:b","ID:c","ID:d","ID:e","ID:f","ID:g" ,"ID:h","ID:i"}; int[] images={R.drawable.man1,R.drawable.man2,R.drawable.man3,R.drawable.man4,R.drawable.man5, R.drawable.man6,R.drawable.man7,R.drawable.man8,R.drawable.man9}; String[] messages={"[99 items] The dialogue with a is temporarily unavailable. It is recommended that you come back next time.","[99 items] The dialogue with b is temporarily unavailable. It is recommended that you come again next time.","[99 items] ] The conversation with c is temporarily unavailable. It is recommended that you come back next time.","[99 items] The conversation with d is temporarily unavailable. It is recommended that you come back next time.","[99 items] The conversation with e is temporarily unavailable. It is recommended that you come back next time. Please come back next time","[99 items] The conversation with f is temporarily unavailable. It is recommended that you come back next time.","[99 items] The conversation with g is temporarily unavailable. It is recommended that you come again next time.","[99 items] ]The conversation with h is temporarily unavailable. It is recommended that you come back next time.","[99 items]The conversation with i is temporarily unavailable. It is recommended that you come back next time."}; List<Map<String,Object>> items=new ArrayList<Map<String,Object>>(); for(int i=0;i<names.length;i + + ){ Map<String,Object> item=new HashMap<String, Object>(); item.put("name",names[i]); item.put("message",messages[i]); item.put("image",images[i]); items.add(item); }
Perform data binding in Myadapter.java
Bind the list item data in the adapter to each list item’s view. In this way, when RecyclerView displays the list, each list item will display its corresponding data. This data binding method allows us to dynamically update the view content of the list items based on different data sources.
String name=list1.get(position).get("name").toString(); String message=list1.get(position).get("message").toString(); int image=Integer.parseInt(list1.get(position).get("image").toString()); holder.ImageView.setImageResource(image); holder.nameTextView.setText(name); holder.messageTextView.setText(message);
Obtain references to each view component in the layout file through the findViewById() method
public class Myholder extends RecyclerView.ViewHolder{ ImageView ImageView; TextView nameTextView; TextView messageTextView; public Myholder(@NonNull View itemView) { super(itemView); ImageView=itemView.findViewById(R.id.tximageView); nameTextView=itemView.findViewById(R.id.nameTextView); messageTextView=itemView.findViewById(R.id.messageTextView); } }
There are three components, so the code part corresponds to three, 1 picture and 2 text
4. Page jump
The intent class will be used here, which can achieve connectivity and interaction between components.
To realize that clicking a list item jumps to a new page and returns from the new page, you need to set a click event listener for the root layout of the list item in the onBindViewHolder() method of the Myadapter.java file and start a new one in the listener. activity and pass the corresponding data.
holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Create an Intent object to start a new activity Intent intent = new Intent(context1, DetailActivity.class); intent.putExtra("name", name); intent.putExtra("message", message); // Start new activity context1.startActivity(intent); } });
This corresponds to the content of the interface after the jump. It only has two components: name and chat content, so the code part is two
After the data is passed here, the jump page needs to receive the data.
In DetailActivity.java, you need to obtain the passed data and display it in the corresponding view component. At the same time, set a click event listener for the return button, and call the finish() method in the click event to return to the previous Activity.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); //Display data in the corresponding view TextView nameTextView = findViewById(R.id.detaileName); TextView messageTextView = findViewById(R.id.detileMessage); // Get the passed data String name = getIntent().getStringExtra("name"); String message = getIntent().getStringExtra("message"); //Display Data nameTextView.setText(name); messageTextView.setText(message); // Return button click event Button returnButton = findViewById(R.id.returnButton); returnButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //End the current activity and return to the previous activity finish(); } }); }
3. Homework summary
The homework this time was still a bit rushed. Sometimes I may be too obsessed with details, so I hesitated to start. I struggled with the details for a long time, which led to the overall slow progress. In the future, I should focus on the overall framework first and resolve the main contradictions first. , put the details later.
Through this assignment, I found that I am still confused about the implementation of various sections and specific functions. It is not feasible to just follow the teacher to type the code without establishing my own understanding of each step. I still have to do it myself and constantly explore to understand. The logic between the various sections.
We must learn to use chatgpt. The rapid iteration of large models has also changed our identity. I think we will be more like an architect and behind-the-scenes designer in the future, informing gpt of our own needs, and leaving the basic code statements to gpt for implementation. But this also has a premise. You must first understand the logic of each part and be able to accurately describe your needs, otherwise you will really let GPT take the lead.
A profound thought: If you can’t write, it’s because you haven’t understood the basic principles. That’s true.
Source code address: Rain hits the bamboo pole half a foot high/Android2