帮我生成一段完整的Android代码,运用静态fragment,用两个按钮切换两段新闻,两段新闻用new1和new2代替。
时间: 2024-05-15 10:16:14 浏览: 300
以下是使用静态fragment实现在两个按钮之间切换新闻的完整Android代码:
MainActivity.java
```
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnNew1, btnNew2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNew1 = findViewById(R.id.btn_new1);
btnNew2 = findViewById(R.id.btn_new2);
// 设置默认Fragment
setDefaultFragment();
// 监听按钮点击事件
btnNew1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new New1Fragment());
}
});
btnNew2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new New2Fragment());
}
});
}
// 设置默认Fragment
private void setDefaultFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, new New1Fragment());
transaction.commit();
}
// 切换Fragment
private void replaceFragment(BaseFragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
}
}
```
BaseFragment.java
```
import androidx.fragment.app.Fragment;
public abstract class BaseFragment extends Fragment {
// 子类实现该方法,返回布局ID
protected abstract int getLayoutId();
}
```
New1Fragment.java
```
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class New1Fragment extends BaseFragment {
@Override
protected int getLayoutId() {
return R.layout.fragment_new1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getLayoutId(), container, false);
}
}
```
New2Fragment.java
```
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class New2Fragment extends BaseFragment {
@Override
protected int getLayoutId() {
return R.layout.fragment_new2;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getLayoutId(), container, false);
}
}
```
activity_main.xml
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_new1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/btn_new2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New2"
android:layout_alignTop="@id/btn_new1"
android:layout_alignRight="@id/btn_new1"
android:layout_marginRight="50dp"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btn_new1"
android:layout_marginTop="50dp"/>
</RelativeLayout>
```
fragment_new1.xml
```
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New1"/>
```
fragment_new2.xml
```
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New2"/>
```
阅读全文