В моем приложении android у меня есть тысячи изображений (предустановка), и у каждого изображения есть кнопка «Поделиться» и кнопка «Поделиться в Whats-App», определенная под ней. Я хочу определить одну функцию для каждой кнопки «Поделиться» (Whats-app и Другие приложения). Но если я установлю на-клики для каждого изображения, оно станет огромным, поэтому, пожалуйста, помогите, как я могу установить одну функцию и определить, какая кнопка обмена изображения была нажата и поделиться этим изображением.
Я приложил код, описывающий моя java и активность:
MAINACTIVITY. JAVA:
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.text.MessageFormat;
public class BD1 extends AppCompatActivity {
ImageButton like;
ImageView img;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bd_1);
like=findViewById(R.id.likes);
}
public void likes(View view)
{ count++;
TextView text=findViewById(R.id.tv_likes);
if(count==1)
text.setText(count+" Like");
else
text.setText(count+" Likes");
Toast.makeText( BD1.this,"Image liked", Toast.LENGTH_LONG).show();
// tv_likes.setText("Likes: " +(count));
}
public void whatsap(View view) {
switch(view.getId())
{
}
Uri imgUri = Uri.parse(pictureFile.getAbsolutePath());
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
}
}
активность. xml:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="@string/op1"
android:textColor="#FFC107"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/scrollView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:fillViewport="true"
android:paddingStart="2dp"
android:paddingLeft="2dp"
android:paddingEnd="2dp"
android:paddingRight="2dp"
android:paddingBottom="10dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:contentDescription="@string/cd"
android:layout_width="match_parent"
android:layout_height="384dp"
app:srcCompat="@mipmap/c1" />
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
tools:layout_editor_absoluteY="36dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="#FFFFFF">
<ImageButton
android:contentDescription="@string/cd"
android:id="@+id/likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:onClick="likes"
app:srcCompat="@mipmap/smile_round" />
<TextView
android:id="@+id/tv_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="30dp"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:paddingStart="30dp"
android:textSize="20sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="30dp"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:paddingStart="30dp"/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#FFFFFF"
android:contentDescription="@string/cd"
android:gravity="center"
android:onClick="whatsap"
app:srcCompat="@mipmap/whatsap_round" />
<ImageButton
android:contentDescription="@string/cd"
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#FFFFFF"
app:srcCompat="@mipmap/share_round" />
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/imageView1"
android:contentDescription="@string/cd"
android:layout_width="match_parent"
android:layout_height="384dp"
app:srcCompat="@mipmap/c2" />
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
tools:layout_editor_absoluteY="36dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="#FFFFFF">
<ImageButton
android:contentDescription="@string/cd"
android:id="@+id/l2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:onClick="likes"
app:srcCompat="@mipmap/smile_round" />
<TextView
android:id="@+id/tv_likes1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="30dp"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:paddingStart="30dp"
android:textSize="20sp" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="30dp"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:paddingStart="30dp"/>
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#FFFFFF"
android:contentDescription="@string/cd"
android:gravity="center"
android:onClick="whatsap"
app:srcCompat="@mipmap/whatsap_round" />
<ImageButton
android:contentDescription="@string/cd"
android:id="@+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#FFFFFF"
app:srcCompat="@mipmap/share_round" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>