public class MainActivity extends BaseActivity {
private SliderImageLayout mSliderImageLayout;
private RecyclerView mRecyclerView;
private CateAdapter mCateAdapter;
private AutoScrollTextView mAutoScrollTv;
private EditText mSearchView;
private int titleBarHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initData();
initEvent();
}
private void initView()
{
mSliderImageLayout = (SliderImageLayout) findViewById(R.id.slider_layout);
mRecyclerView = (RecyclerView) findViewById(R.id.cate_list_view);
mSearchView = (EditText) findViewById(R.id.search_view);
mAutoScrollTv = (AutoScrollTextView) findViewById(R.id.auto_text);
initState();
titleBarHeight = getStatusBarHeight();
setMargins(mSearchView , 80 , titleBarHeight , 80 , 0);
}
private void initData()
{
mSliderImageLayout.setSliderImg(R.drawable.pic1);
mSliderImageLayout.setTitleText("衣服");
mSliderImageLayout.setSliderImg(R.drawable.pic2);
mSliderImageLayout.setTitleText("手机");
mSliderImageLayout.setSliderImg(R.drawable.pic3);
mSliderImageLayout.setTitleText("电脑");
mSliderImageLayout.setSliderImg(R.drawable.pic4);
mSliderImageLayout.setTitleText("手表");
mCateAdapter = new CateAdapter(this , Contacts.categoryDataS);
mRecyclerView.setLayoutManager(new GridLayoutManager(this , 4));
mRecyclerView.setAdapter(mCateAdapter);
mAutoScrollTv.init(getWindowManager());
mAutoScrollTv.startScroll();
}
private void initEvent()
{
mSliderImageLayout.setOnSliderItemClickListener(new SliderImageLayout.OnSliderItemClickListener()
{
@Override
public void onSliderItemClick(int position)
{
Toast.makeText(MainActivity.this , "当前选择的位置是: " + position , Toast.LENGTH_SHORT).show();
}
});
mCateAdapter.setOnItemClickListener(new CateAdapter.OnItemClickListener()
{
@Override
public void onItemClick(View view, int position)
{
Toast.makeText(MainActivity.this , "当前选择的位置是:" + position , Toast.LENGTH_SHORT).show();
}
});
}
}
public class BaseActivity extends AppCompatActivity {
protected void initState()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
protected int getStatusBarHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public void setMargins(View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}
}
public class CategoryEntity
{
private int currentId;
private int picId;
private String cateName;
public CategoryEntity(int currentId, int picId, String cateName)
{
this.currentId = currentId;
this.picId = picId;
this.cateName = cateName;
}
public int getCurrentId() {
return currentId;
}
public void setCurrentId(int currentId) {
this.currentId = currentId;
}
public int getPicId() {
return picId;
}
public void setPicId(int picId) {
this.picId = picId;
}
public String getCateName() {
return cateName;
}
public void setCateName(String cateName) {
this.cateName = cateName;
}
}
public class Contacts
{
public static final List<CategoryEntity> categoryDataS = new ArrayList<>()
static
{
CategoryEntity categoryEntity1 = new CategoryEntity(1 , R.drawable.icon_favorite , "收藏")
categoryDataS.add(categoryEntity1)
CategoryEntity categoryEntity2 = new CategoryEntity(1 , R.drawable.icon_list_o , "列表")
categoryDataS.add(categoryEntity2)
CategoryEntity categoryEntity3 = new CategoryEntity(1 , R.drawable.icon_location , "定位")
categoryDataS.add(categoryEntity3)
CategoryEntity categoryEntity4 = new CategoryEntity(1 , R.drawable.icon_msg , "评论")
categoryDataS.add(categoryEntity4)
CategoryEntity categoryEntity5 = new CategoryEntity(1 , R.drawable.icon_favorite , "收藏")
categoryDataS.add(categoryEntity5)
CategoryEntity categoryEntity6 = new CategoryEntity(1 , R.drawable.icon_list_o , "表格")
categoryDataS.add(categoryEntity6)
CategoryEntity categoryEntity7 = new CategoryEntity(1 , R.drawable.icon_location , "位置")
categoryDataS.add(categoryEntity7)
CategoryEntity categoryEntity8 = new CategoryEntity(1 , R.drawable.icon_msg , "消息")
categoryDataS.add(categoryEntity8)
}
}