parent
a12c42c55c
commit
baea25e6dc
@ -1,39 +1,60 @@ |
||||
package com.community.pocket.ui.main; |
||||
|
||||
import android.os.Bundle; |
||||
import android.view.LayoutInflater; |
||||
import android.os.Build; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.widget.TextView; |
||||
import android.widget.Button; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.Observer; |
||||
import androidx.annotation.RequiresApi; |
||||
import androidx.navigation.NavController; |
||||
import androidx.navigation.Navigation; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.ui.BaseFragment; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* 二级菜单基础框架 |
||||
*/ |
||||
public abstract class MainFragment extends BaseFragment { |
||||
|
||||
@NotNull |
||||
@Override |
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
||||
|
||||
View root = super.onCreateView(inflater, container, savedInstanceState); |
||||
final TextView textView = root.findViewById(viewId()); |
||||
MutableLiveData<String> mText = new MutableLiveData<>(); |
||||
mText.setValue(this.getClass().getName()); |
||||
mText.observe(getViewLifecycleOwner(), new Observer<String>() { |
||||
@Override |
||||
public void onChanged(@Nullable String s) { |
||||
textView.setText(s); |
||||
/** |
||||
* @return FragmentContainerView id |
||||
*/ |
||||
protected abstract int fragmentId(); |
||||
|
||||
private NavController navController; |
||||
|
||||
/** |
||||
* @return 路由控制器 |
||||
*/ |
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) |
||||
protected NavController ctrl() { |
||||
if (navController == null) { |
||||
navController = Navigation.findNavController(Objects.requireNonNull(getView()).findViewById(fragmentId())); |
||||
} |
||||
return navController; |
||||
} |
||||
}); |
||||
|
||||
return root; |
||||
/** |
||||
* 点击按钮改变字体颜色 |
||||
*/ |
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) |
||||
protected void changeColor(View view) { |
||||
int color = getResources().getColor(R.color.button_unchecked); |
||||
for (int id : button_ids()) { |
||||
View v = Objects.requireNonNull(getView()).findViewById(id); |
||||
v.setBackgroundColor(color); |
||||
} |
||||
|
||||
if (view instanceof Button) { |
||||
Button button = (Button) view; |
||||
button.setBackgroundColor(getResources().getColor(R.color.colorAccent)); |
||||
} |
||||
} |
||||
|
||||
protected abstract int viewId(); |
||||
/** |
||||
* 按钮组id |
||||
*/ |
||||
protected abstract int[] button_ids(); |
||||
} |
||||
|
@ -0,0 +1,44 @@ |
||||
package com.community.pocket.ui.main; |
||||
|
||||
import android.os.Bundle; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.widget.TextView; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.Observer; |
||||
|
||||
import com.community.pocket.ui.BaseFragment; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
/** |
||||
* 主界面框架 |
||||
*/ |
||||
//TODO 测试类
|
||||
public abstract class TestMainFragment extends BaseFragment { |
||||
|
||||
@NotNull |
||||
@Override |
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
||||
|
||||
//文本框打印类信息
|
||||
View root = super.onCreateView(inflater, container, savedInstanceState); |
||||
final TextView textView = root.findViewById(viewId()); |
||||
MutableLiveData<String> mText = new MutableLiveData<>(); |
||||
mText.setValue(this.getClass().getName()); |
||||
mText.observe(getViewLifecycleOwner(), new Observer<String>() { |
||||
@Override |
||||
public void onChanged(@Nullable String s) { |
||||
textView.setText(s); |
||||
} |
||||
}); |
||||
|
||||
return root; |
||||
} |
||||
|
||||
protected abstract int viewId(); |
||||
} |
@ -0,0 +1,62 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import android.os.Build; |
||||
import android.view.View; |
||||
|
||||
import androidx.annotation.RequiresApi; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.ui.main.MainFragment; |
||||
|
||||
import org.xutils.view.annotation.ContentView; |
||||
import org.xutils.view.annotation.Event; |
||||
|
||||
/** |
||||
* 论坛框架 |
||||
*/ |
||||
@ContentView(R.layout.forum) |
||||
public class ForumFragment extends MainFragment { |
||||
|
||||
//跳转热榜界面
|
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) |
||||
@Event(value = R.id.forum_hot) |
||||
private void hot(View view) { |
||||
ctrl().navigate(R.id.forumHotFragment); |
||||
changeColor(view); |
||||
} |
||||
|
||||
//跳转最新界面
|
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) |
||||
@Event(value = R.id.forum_new) |
||||
private void _new(View view) { |
||||
ctrl().navigate(R.id.forumNewFragment); |
||||
changeColor(view); |
||||
} |
||||
|
||||
|
||||
//跳转发帖界面
|
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) |
||||
@Event(value = R.id.forum_post) |
||||
private void post(View view) { |
||||
ctrl().navigate(R.id.forumPostFragment); |
||||
changeColor(view); |
||||
} |
||||
|
||||
//跳转我的帖子界面
|
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT) |
||||
@Event(value = R.id.forum_my) |
||||
private void my(View view) { |
||||
ctrl().navigate(R.id.forumMyFragment); |
||||
changeColor(view); |
||||
} |
||||
|
||||
@Override |
||||
protected int fragmentId() { |
||||
return R.id.fragment; |
||||
} |
||||
|
||||
@Override |
||||
protected int[] button_ids() { |
||||
return new int[]{R.id.forum_hot, R.id.forum_new, R.id.forum_post, R.id.forum_my}; |
||||
} |
||||
} |
@ -0,0 +1,20 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.ui.main.TestMainFragment; |
||||
|
||||
import org.xutils.view.annotation.ContentView; |
||||
|
||||
/** |
||||
* 热门帖子 |
||||
*/ |
||||
@ContentView(R.layout.forum_hot_fragment) |
||||
public class ForumHotFragment extends TestMainFragment { |
||||
|
||||
@Override |
||||
protected int viewId() { |
||||
return R.id.text_hot; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,7 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
public class ForumHotViewModel extends ViewModel { |
||||
// TODO: Implement the ViewModel
|
||||
} |
@ -0,0 +1,17 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.ui.main.TestMainFragment; |
||||
|
||||
import org.xutils.view.annotation.ContentView; |
||||
|
||||
/** |
||||
* 我的帖子 |
||||
*/ |
||||
@ContentView(R.layout.forum_my_fragment) |
||||
public class ForumMyFragment extends TestMainFragment { |
||||
@Override |
||||
protected int viewId() { |
||||
return R.id.text_my; |
||||
} |
||||
} |
@ -0,0 +1,7 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
public class ForumMyViewModel extends ViewModel { |
||||
// TODO: Implement the ViewModel
|
||||
} |
@ -0,0 +1,19 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.ui.main.TestMainFragment; |
||||
|
||||
import org.xutils.view.annotation.ContentView; |
||||
|
||||
/** |
||||
* 最新帖子 |
||||
*/ |
||||
@ContentView(R.layout.forum_new_fragment) |
||||
public class ForumNewFragment extends TestMainFragment { |
||||
|
||||
@Override |
||||
protected int viewId() { |
||||
return R.id.text_new; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,7 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
public class ForumNewViewModel extends ViewModel { |
||||
// TODO: Implement the ViewModel
|
||||
} |
@ -0,0 +1,19 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.ui.main.TestMainFragment; |
||||
|
||||
import org.xutils.view.annotation.ContentView; |
||||
|
||||
/** |
||||
* 发送帖子 |
||||
*/ |
||||
@ContentView(R.layout.forum_post_fragment) |
||||
public class ForumPostFragment extends TestMainFragment { |
||||
|
||||
@Override |
||||
protected int viewId() { |
||||
return R.id.text_post; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,7 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
public class ForumPostViewModel extends ViewModel { |
||||
// TODO: Implement the ViewModel
|
||||
} |
@ -1,14 +1,14 @@ |
||||
package com.community.pocket.ui.main.ui.home; |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import androidx.lifecycle.LiveData; |
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
public class HomeViewModel extends ViewModel { |
||||
public class ForumViewModel extends ViewModel { |
||||
|
||||
private MutableLiveData<String> mText; |
||||
|
||||
public HomeViewModel() { |
||||
public ForumViewModel() { |
||||
mText = new MutableLiveData<>(); |
||||
mText.setValue("This is home fragment"); |
||||
} |
@ -1,15 +0,0 @@ |
||||
package com.community.pocket.ui.main.ui.home; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.ui.main.MainFragment; |
||||
|
||||
import org.xutils.view.annotation.ContentView; |
||||
|
||||
@ContentView(R.layout.fragment_home) |
||||
public class HomeFragment extends MainFragment { |
||||
|
||||
@Override |
||||
protected int viewId() { |
||||
return R.id.text_home; |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context=".ui.main.ui.forum.ForumFragment"> |
||||
|
||||
<include |
||||
android:id="@+id/titlebar" |
||||
layout="@layout/titlebar" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toTopOf="parent" |
||||
tools:layout_constraintEnd_toEndOf="parent" /> |
||||
|
||||
<androidx.fragment.app.FragmentContainerView |
||||
android:id="@+id/fragment" |
||||
android:name="androidx.navigation.fragment.NavHostFragment" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="0dp" |
||||
android:background="@color/colorAccent" |
||||
app:defaultNavHost="true" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintEnd_toEndOf="parent" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toBottomOf="@id/titlebar" |
||||
app:navGraph="@navigation/forum_navigation" /> |
||||
</androidx.constraintlayout.widget.ConstraintLayout> |
@ -0,0 +1,13 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context=".ui.main.ui.forum.ForumHotFragment"> |
||||
|
||||
<TextView |
||||
android:id="@+id/text_hot" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
</FrameLayout> |
@ -0,0 +1,13 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context=".ui.main.ui.forum.ForumMyFragment"> |
||||
|
||||
<TextView |
||||
android:id="@+id/text_my" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" /> |
||||
|
||||
</FrameLayout> |
@ -0,0 +1,13 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context=".ui.main.ui.forum.ForumNewFragment"> |
||||
|
||||
<TextView |
||||
android:id="@+id/text_new" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
</FrameLayout> |
@ -0,0 +1,13 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context=".ui.main.ui.forum.ForumPostFragment"> |
||||
|
||||
<TextView |
||||
android:id="@+id/text_post" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" /> |
||||
|
||||
</FrameLayout> |
@ -0,0 +1,56 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:id="@+id/forum_titlebar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content"> |
||||
|
||||
<Button |
||||
android:id="@+id/forum_hot" |
||||
style="?android:attr/buttonBarStyle" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:background="@color/colorAccent" |
||||
android:text="@string/forum_hot" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toTopOf="parent" /> |
||||
|
||||
<Button |
||||
android:id="@+id/forum_new" |
||||
style="?android:attr/buttonBarStyle" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:background="@color/button_unchecked" |
||||
android:text="@string/forum_new" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintStart_toEndOf="@id/forum_hot" |
||||
app:layout_constraintTop_toTopOf="parent" /> |
||||
|
||||
<Button |
||||
android:id="@+id/forum_post" |
||||
style="?android:attr/buttonBarStyle" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:background="@color/button_unchecked" |
||||
android:text="@string/forum_post" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintStart_toEndOf="@id/forum_new" |
||||
app:layout_constraintTop_toTopOf="parent" /> |
||||
|
||||
<Button |
||||
android:id="@+id/forum_my" |
||||
style="?android:attr/buttonBarStyle" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:background="@color/button_unchecked" |
||||
android:text="@string/forum_my" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintEnd_toEndOf="parent" |
||||
app:layout_constraintStart_toEndOf="@id/forum_post" |
||||
app:layout_constraintTop_toTopOf="parent" /> |
||||
</LinearLayout> |
@ -1,22 +0,0 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context=".ui.main.ui.home.HomeFragment"> |
||||
|
||||
<TextView |
||||
android:id="@+id/text_home" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginStart="8dp" |
||||
android:layout_marginTop="8dp" |
||||
android:layout_marginEnd="8dp" |
||||
android:textAlignment="center" |
||||
android:textSize="20sp" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintEnd_toEndOf="parent" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toTopOf="parent" /> |
||||
</androidx.constraintlayout.widget.ConstraintLayout> |
@ -0,0 +1,29 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<navigation xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:id="@+id/forum_navigation" |
||||
app:startDestination="@id/forumHotFragment" |
||||
tools:ignore="UnusedNavigation"> |
||||
|
||||
<fragment |
||||
android:id="@+id/forumHotFragment" |
||||
android:name="com.community.pocket.ui.main.ui.forum.ForumHotFragment" |
||||
android:label="forum_hot_fragment" |
||||
tools:layout="@layout/forum_hot_fragment" /> |
||||
<fragment |
||||
android:id="@+id/forumNewFragment" |
||||
android:name="com.community.pocket.ui.main.ui.forum.ForumNewFragment" |
||||
android:label="forum_new_fragment" |
||||
tools:layout="@layout/forum_new_fragment" /> |
||||
<fragment |
||||
android:id="@+id/forumPostFragment" |
||||
android:name="com.community.pocket.ui.main.ui.forum.ForumPostFragment" |
||||
android:label="forum_post_fragment" |
||||
tools:layout="@layout/forum_post_fragment" /> |
||||
<fragment |
||||
android:id="@+id/forumMyFragment" |
||||
android:name="com.community.pocket.ui.main.ui.forum.ForumMyFragment" |
||||
android:label="forum_my_fragment" |
||||
tools:layout="@layout/forum_my_fragment" /> |
||||
</navigation> |
@ -0,0 +1,5 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<resources> |
||||
<color name="colorBlack">#000000</color> |
||||
<color name="button_unchecked">#00BCD4</color> |
||||
</resources> |
@ -0,0 +1,5 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<resources> |
||||
<color name="colorBlack">#000000</color> |
||||
<color name="button_unchecked">#00BCD4</color> |
||||
</resources> |
Loading…
Reference in new issue