2024.4.24

一点都不难發表於2024-06-28

今天寫了一下發帖功能的Mainactivity.java檔案
package com.example.photo1;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private EditText editTextPost;
private RecyclerView recyclerViewSelectedImages, recyclerViewPosts;
private List imageUris = new ArrayList<>();
private List posts = new ArrayList<>();
private ImageAdapter imageAdapter;
private PostAdapter postAdapter;

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

    editTextPost = findViewById(R.id.editTextPost);
    Button buttonSelectImages = findViewById(R.id.buttonSelectImages);
    Button buttonUploadPost = findViewById(R.id.buttonUploadPost);
    recyclerViewSelectedImages = findViewById(R.id.recyclerViewSelectedImages);
    recyclerViewPosts = findViewById(R.id.recyclerViewPosts);

    // Setup RecyclerView for selected images
    recyclerViewSelectedImages.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    imageAdapter = new ImageAdapter(imageUris, this);
    recyclerViewSelectedImages.setAdapter(imageAdapter);

    // Setup RecyclerView for posts
    recyclerViewPosts.setLayoutManager(new LinearLayoutManager(this));
    postAdapter = new PostAdapter(posts, this);
    recyclerViewPosts.setAdapter(postAdapter);

    // Image selection launcher
    ActivityResultLauncher<String[]> imagePickerLauncher = registerForActivityResult(
            new ActivityResultContracts.OpenMultipleDocuments(),
            uris -> {
                if (uris != null && !uris.isEmpty()) {
                    imageUris.clear();
                    imageUris.addAll(uris);
                    imageAdapter.notifyDataSetChanged();
                }
            });

    // Select images button event
    buttonSelectImages.setOnClickListener(v -> imagePickerLauncher.launch(new String[]{"image/*"}));

    // Upload post button event
    buttonUploadPost.setOnClickListener(v -> uploadPost());
}

private void uploadPost() {
    String postText = editTextPost.getText().toString().trim();
    if (!postText.isEmpty() || !imageUris.isEmpty()) {
        Post newPost = new Post(postText, new ArrayList<>(imageUris));
        posts.add(newPost);
        postAdapter.notifyDataSetChanged();
        editTextPost.setText("");
        imageUris.clear();
        imageAdapter.notifyDataSetChanged();

        Toast.makeText(this, "發表成功!", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "請輸入文字內容或插入圖片!", Toast.LENGTH_SHORT).show();
    }
}

}