Skip to content

Commit

Permalink
add button click on ViewPager
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjobs committed Mar 11, 2020
1 parent 4313f3a commit 6f93dba
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
12 changes: 8 additions & 4 deletions ViewPagerTest/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ViewPagerTest/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -14,6 +16,9 @@ public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private List<View> views;
private MyNewAdapter myNewAdapter;

private Button button1,button2,button3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -30,6 +35,31 @@ protected void onCreate(Bundle savedInstanceState) {
views.add(view2);
views.add(view3);

button1 = view1.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"来自页面1",Toast.LENGTH_SHORT).show();
}
});

button2 = view2.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"来自页面2",Toast.LENGTH_SHORT).show();
}
});

button3 = view3.findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"来自页面3",Toast.LENGTH_SHORT).show();
}
});


myNewAdapter = new MyNewAdapter(views);
viewPager.setAdapter(myNewAdapter);

Expand Down
6 changes: 6 additions & 0 deletions ViewPagerTest/app/src/main/res/layout/a1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一个界面" />

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
6 changes: 6 additions & 0 deletions ViewPagerTest/app/src/main/res/layout/a2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二个界面" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
6 changes: 6 additions & 0 deletions ViewPagerTest/app/src/main/res/layout/a3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第三个界面" />

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

0 comments on commit 6f93dba

Please sign in to comment.