热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

带指示器的viewpager----ViewPagerIndicator的改进版

自己实现了一个更加方便的ViewPagerIndicator,希望大家批评指正,代码如下:packagecom.ningfengview;importjava.util.ArrayList;imp

自己实现了一个更加方便的ViewPagerIndicator,希望大家批评指正,代码如下:

package com.ningfengview;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NFViewPager extends LinearLayout {

private HorizontalScrollView mScrollViewHeader = null;
private LinearLayout mHeaderCOntainer= null;
private LinearLayout mTitleCOntainer= null;
private LinearLayout mIndecatorCOntainer= null;
private LinearLayout mIndecatorContainer2 = null;
private LinearLayout mIndecator = null;
private LinearLayout mIndecator2 = null;
private List mTitleList = null;
List mFragmentList = null;
private int mHorizOntialMargin= 0;
private ViewPager mViewPager = null;
private int titleSelectedColor = 0;
private int titleUnselectedColor = 0;
private Context cOntext= null;
private NFViewPagerInterface nfinterface=null;

public NFViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.cOntext= context;
this.mTitleList = new ArrayList();
this.mScrollViewHeader = new HorizontalScrollView(context);
this.mScrollViewHeader.setHorizontalScrollBarEnabled(false);
this.mHeaderCOntainer= new LinearLayout(context);
this.mHeaderContainer.setOrientation(LinearLayout.VERTICAL);

this.mViewPager = new ViewPager(context);

this.mTitleCOntainer= new LinearLayout(context);
this.mTitleContainer.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecatorCOntainer= new LinearLayout(context);
this.mIndecatorContainer.setOrientation(LinearLayout.HORIZONTAL);

this.mIndecatorContainer2 = new LinearLayout(context);
this.mIndecatorContainer2.setOrientation(LinearLayout.HORIZONTAL);

this.mIndecator = new LinearLayout(context);
this.mIndecator.setOrientation(LinearLayout.HORIZONTAL);

this.mIndecator2 = new LinearLayout(context);
this.mIndecator2.setOrientation(LinearLayout.HORIZONTAL);

}
public interface NFViewPagerInterface{
public void onPageChanged(int currentPage);

}
public class NFOnClickListener implements OnClickListener {
int position = 0;

public NFOnClickListener(int position) {
this.position = position;
}

@Override
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
}

public void init(NFViewPagerInterface inter,int id,List textList, List frgList,
FragmentManager frm, int indecatorHeight, int titleMarginLeft,
int titleMarginRight, int titleMarginTop, int titleMarginBottom,
int indecatorBackgroundColor, int indecatorColor,
int titleUnselectedColor, int titleSelectedColor,
float titleTextSize) {
this.mViewPager.setId(id);
this.nfinterface=inter;
this.titleSelectedColor = titleSelectedColor;
this.titleUnselectedColor = titleUnselectedColor;
this.mFragmentList = frgList;
LayoutParams titleParam = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
// titleParam.setMargins(titleMarginHorizontal, titleMarginVertical,
// titleMarginHorizontal, titleMarginVertical);

for (int i = 0; i
TextView texttitle = new TextView(context);
texttitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleTextSize);
texttitle.setText(textList.get(i));
texttitle.setOnClickListener(new NFOnClickListener(i));
texttitle.setPadding(titleMarginLeft, titleMarginTop,
titleMarginRight, titleMarginBottom);
if (i ==0) {
texttitle.setTextColor(titleSelectedColor);
} else {
texttitle.setTextColor(titleUnselectedColor);
}
this.mTitleList.add(texttitle);
this.mTitleContainer.addView(texttitle, titleParam);
}
LayoutParams titleCOntainerParam= new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.mHeaderContainer
.addView(this.mTitleContainer, titleContainerParam);

this.mIndecator.setBackgroundColor(indecatorColor);
this.mIndecator2.setBackgroundColor(indecatorColor);

measureView(this.mTitleList.get(0), 2);
LayoutParams indecatorParam = new LayoutParams(this.mTitleList.get(0)
.getMeasuredWidth(), indecatorHeight);
this.mIndecatorContainer.addView(this.mIndecator, indecatorParam);


LayoutParams indecatorParam2 = new LayoutParams(this.mTitleList.get(0)
.getMeasuredWidth(), indecatorHeight*4);
this.mIndecatorContainer2.addView(this.mIndecator2, indecatorParam2);

this.mIndecatorContainer.setBackgroundColor(indecatorBackgroundColor);
LayoutParams indecatorCOntainerParam= new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

LayoutParams indecatorContainerParam2 = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.mHeaderContainer.addView(this.mIndecatorContainer2,
indecatorContainerParam2);
this.mHeaderContainer.addView(this.mIndecatorContainer,
indecatorContainerParam);

LayoutParams headerCOntainerParam= new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.mScrollViewHeader.addView(this.mHeaderContainer,
headerContainerParam);

LayoutParams scrollViewHeaderParam = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.addView(mScrollViewHeader, scrollViewHeaderParam);

LayoutParams viewPagerParam = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
this.mViewPager.setAdapter(new MyFragmentPagerAdapter(frm,
this.mFragmentList));
this.mViewPager.setCurrentItem(0);
this.mViewPager
.setOnPageChangeListener(new NFViewPagerOnPageChangeListener());
this.addView(this.mViewPager, viewPagerParam);

}

public ViewPager getViewPager()
{

return this.mViewPager;
}

public void setNFCurrentItem(int index)
{
mViewPager.setCurrentItem(index);
int len = mTitleList.size();
for (int i = 0; i if (index == i) {
mTitleList.get(i).setTextColor(titleSelectedColor);
} else {
mTitleList.get(i).setTextColor(titleUnselectedColor);
}
}

}

public class NFViewPagerOnPageChangeListener implements
OnPageChangeListener {

private int position = 0;

@Override
public void onPageScrollStateChanged(int arg0) {



if (arg0 == 0) {
int len = mTitleList.size();
for (int i = 0; i if (this.position == i) {
mTitleList.get(i).setTextColor(titleSelectedColor);
} else {
mTitleList.get(i).setTextColor(titleUnselectedColor);
}
}

}
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {


mIndecatorContainer.scrollTo(-mTitleList.get(arg0).getLeft()
- (int) (mTitleList.get(arg0).getWidth() * arg1), 0);

mIndecatorContainer2.scrollTo(-mTitleList.get(arg0).getLeft()
- (int) (mTitleList.get(arg0).getWidth() * arg1), 0);
if (arg0
int tempwidth = mTitleList.get(arg0).getWidth()
+ (int) ((mTitleList.get(arg0 + 1).getWidth() - mTitleList
.get(arg0).getWidth()) * arg1);
LayoutParams indecatorParam = (LayoutParams) mIndecator
.getLayoutParams();
indecatorParam.width = tempwidth;
mIndecator.setLayoutParams(indecatorParam);

LayoutParams indecatorParam2 = (LayoutParams) mIndecator2
.getLayoutParams();
indecatorParam2.width = tempwidth;
mIndecator2.setLayoutParams(indecatorParam2);
} else {
}
if (arg0 > 0) {
mScrollViewHeader
.scrollTo(
mTitleList.get(arg0 - 1).getLeft()
+ (int) (mTitleList.get(arg0 - 1)
.getWidth() * arg1), 0);
}
}

@Override
public void onPageSelected(int arg0) {
this.position = arg0;
nfinterface.onPageChanged(arg0);
}
}

public static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
public List list;

public MyFragmentPagerAdapter(FragmentManager fm, List list) {
super(fm);
this.list = list;

}

@Override
public int getCount() {
return list.size();
}

@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}

// 初始化每个页卡选项
@Override
public Object instantiateItem(ViewGroup arg0, int arg1) {
// TODO Auto-generated method stub
return super.instantiateItem(arg0, arg1);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
System.out.println( "**********************************position Destory" + position);
super.destroyItem(container, position, object);
}

}

private void measureView(View child, int flag) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
if (flag == 1) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
} else if (2 == flag) {

p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}

}

这个是每个标题指示器根据文字的大小自动扩展的。还有一个可以指定标题指示器的宽度的,代码如下:

package com.ningfengview;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NFStableWidthViewPager extends LinearLayout {

private HorizontalScrollView mScrollViewHeader = null;
private LinearLayout mHeaderCOntainer= null;
private LinearLayout mTitleCOntainer= null;
private LinearLayout mIndecatorCOntainer= null;

private LinearLayout mIndecatorContainer2=null;

private LinearLayout mIndecator = null;
private LinearLayout mIndecator2 = null;
private List mTitleList = null;
List mFragmentList = null;
private int mHorizOntialMargin= 0;
private ViewPager mViewPager = null;
private int titleSelectedColor = 0;
private int titleUnselectedColor = 0;
private Context cOntext= null;

public NFStableWidthViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.cOntext= context;
this.mTitleList = new ArrayList();
this.mScrollViewHeader = new HorizontalScrollView(context);
this.mScrollViewHeader.setHorizontalScrollBarEnabled(false);
this.mHeaderCOntainer= new LinearLayout(context);
this.mHeaderContainer.setOrientation(LinearLayout.VERTICAL);

this.mViewPager = new ViewPager(context);

this.mTitleCOntainer= new LinearLayout(context);
this.mTitleContainer.setOrientation(LinearLayout.HORIZONTAL);
this.mIndecatorCOntainer= new LinearLayout(context);
this.mIndecatorContainer.setOrientation(LinearLayout.HORIZONTAL);

this.mIndecatorContainer2 = new LinearLayout(context);
this.mIndecatorContainer2.setOrientation(LinearLayout.HORIZONTAL);

this.mIndecator = new LinearLayout(context);
this.mIndecator.setOrientation(LinearLayout.HORIZONTAL);

this.mIndecator2 = new LinearLayout(context);
this.mIndecator2.setOrientation(LinearLayout.HORIZONTAL);

}

public class NFOnClickListener implements OnClickListener {
int position = 0;

public NFOnClickListener(int position) {
this.position = position;
}

@Override
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
}

public void init(int id,List textList, List frgList,
FragmentManager frm, int indecatorHeight, int singleTitleWidth,int marginVertical,
int indecatorBackgroundColor, int indecatorColor,
int titleUnselectedColor, int titleSelectedColor,
float titleTextSize) {
this.mViewPager.setId(id);
this.titleSelectedColor = titleSelectedColor;
this.titleUnselectedColor = titleUnselectedColor;
this.mFragmentList = frgList;
LayoutParams titleParam = new LayoutParams(singleTitleWidth,
LayoutParams.WRAP_CONTENT);
titleParam.setMargins(0, marginVertical,
0, marginVertical);

for (int i = 0; i
TextView texttitle = new TextView(context);
texttitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleTextSize);
texttitle.setText(textList.get(i));
texttitle.setOnClickListener(new NFOnClickListener(i));
texttitle.setGravity(Gravity.CENTER_HORIZONTAL);
//texttitle.setPadding(titleMarginLeft, titleMarginTop,
//titleMarginRight, titleMarginBottom);
if (i ==0) {
texttitle.setTextColor(titleSelectedColor);
} else {
texttitle.setTextColor(titleUnselectedColor);
}
this.mTitleList.add(texttitle);
this.mTitleContainer.addView(texttitle, titleParam);
}
LayoutParams titleCOntainerParam= new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.mHeaderContainer
.addView(this.mTitleContainer, titleContainerParam);

this.mIndecator.setBackgroundColor(indecatorColor);
measureView(this.mTitleList.get(0), 2);
LayoutParams indecatorParam = new LayoutParams(this.mTitleList.get(0)
.getMeasuredWidth(), indecatorHeight);
this.mIndecatorContainer.addView(this.mIndecator, indecatorParam);

this.mIndecator2.setBackgroundColor(indecatorColor);
measureView(this.mTitleList.get(0), 2);
LayoutParams indecatorParam2 = new LayoutParams(this.mTitleList.get(0)
.getMeasuredWidth(), indecatorHeight*4);
this.mIndecatorContainer2.addView(this.mIndecator2, indecatorParam2);

this.mIndecatorContainer.setBackgroundColor(indecatorBackgroundColor);
LayoutParams indecatorCOntainerParam= new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

//this.mIndecatorContainer.setBackgroundColor(indecatorBackgroundColor);
LayoutParams indecatorContainerParam2 = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

this.mHeaderContainer.addView(this.mIndecatorContainer2,
indecatorContainerParam2);

this.mHeaderContainer.addView(this.mIndecatorContainer,
indecatorContainerParam);

LayoutParams headerCOntainerParam= new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.mScrollViewHeader.addView(this.mHeaderContainer,
headerContainerParam);

LayoutParams scrollViewHeaderParam = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.addView(mScrollViewHeader, scrollViewHeaderParam);

LayoutParams viewPagerParam = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
this.mViewPager.setAdapter(new MyFragmentPagerAdapter(frm,
this.mFragmentList));
this.mViewPager.setCurrentItem(0);
this.mViewPager
.setOnPageChangeListener(new NFViewPagerOnPageChangeListener());
this.addView(this.mViewPager, viewPagerParam);

}

public ViewPager getViewPager()
{

return this.mViewPager;
}
public class NFViewPagerOnPageChangeListener implements
OnPageChangeListener {

private int position = 0;

@Override
public void onPageScrollStateChanged(int arg0) {

if (arg0 == 0) {
int len = mTitleList.size();
for (int i = 0; i if (this.position == i) {
mTitleList.get(i).setTextColor(titleSelectedColor);
} else {
mTitleList.get(i).setTextColor(titleUnselectedColor);
}
}

}
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {


mIndecatorContainer.scrollTo(-mTitleList.get(arg0).getLeft()
- (int) (mTitleList.get(arg0).getWidth() * arg1), 0);

mIndecatorContainer2.scrollTo(-mTitleList.get(arg0).getLeft()
- (int) (mTitleList.get(arg0).getWidth() * arg1), 0);
if (arg0
int tempwidth = mTitleList.get(arg0).getWidth()
+ (int) ((mTitleList.get(arg0 + 1).getWidth() - mTitleList
.get(arg0).getWidth()) * arg1);
LayoutParams indecatorParam = (LayoutParams) mIndecator
.getLayoutParams();
indecatorParam.width = tempwidth;
mIndecator.setLayoutParams(indecatorParam);



LayoutParams indecatorParam2 = (LayoutParams) mIndecator2
.getLayoutParams();
indecatorParam2.width = tempwidth;
mIndecator2.setLayoutParams(indecatorParam2);

} else {
}
if (arg0 > 0) {
mScrollViewHeader
.scrollTo(
mTitleList.get(arg0 - 1).getLeft()
+ (int) (mTitleList.get(arg0 - 1)
.getWidth() * arg1), 0);
}
}

@Override
public void onPageSelected(int arg0) {
this.position = arg0;
}
}

public static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
public List list;

public MyFragmentPagerAdapter(FragmentManager fm, List list) {
super(fm);
this.list = list;

}

@Override
public int getCount() {
return list.size();
}

@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}

// 初始化每个页卡选项
@Override
public Object instantiateItem(ViewGroup arg0, int arg1) {
// TODO Auto-generated method stub
return super.instantiateItem(arg0, arg1);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}

}

private void measureView(View child, int flag) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
if (flag == 1) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
} else if (2 == flag) {

p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}

}


使用方法也特别简单,首先布局文件如下:

            android:id="@+id/nfviewpager"
android:layout_
android:layout_
android:orientation="vertical" >

java中的代码如下:

List mTempData = new ArrayList();
List mTempFragments = new ArrayList();
NFViewPager nfViewPager = null;
nfViewPager = (NFViewPager) view
.findViewById(R.id.nfviewpager);
mTempData.add("title1");
mTempData.add("title2");
mTempData.add("title3");
mTempData.add("title4");
mTempData.add("title5");
mTempData.add("title6");
mTempData.add("title7");
mTempData.add("title8");
mTempData.add("title9");
mTempData.add("title10");

for (int i = 0; i <10; i++) {
mTempFragments.add(new TestFrg());
}

nfViewPager.init(new NFViewPagerInterface() {

@Override
public void onPageChanged(int currentPage) {
// TODO Auto-generated method stub

}
},5000, mTempData, mTempFragments,
getChildFragmentManager(), 1, 6* Helper.screenwidth / 100,
6 * Helper.screenwidth / 100,
(int) (1.5 * Helper.screenheight / 200),
(int) (1.5 * Helper.screenheight / 200),
Color.rgb(222, 241, 253), Color.rgb(51, 158, 226),
Color.rgb(102, 102, 102), Color.rgb(51, 158, 226), 16);
nfViewPager.setNFCurrentItem(0);

如果是固定宽度的只需要把NFViewPager换成NFStableWidthViewPager并把初始化代码换成下面的就行
nfViewPager
.init(123,this.mData, this.mFragment,getChildFragmentManager(), 1,Helper.screenwidth/4/*此参数即是每个title的固定宽度*/,8,Color.rgb(211, 237, 253),Color.rgb(51, 158, 226), Color.rgb(102, 102, 102), Color.rgb(51, 158, 226), 16);

是不是比ViewPagerIndicator还好用。欢迎拍砖吐槽



推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • 数组的排序:数组本身有Arrays类中的sort()方法,这里写几种常见的排序方法。(1)冒泡排序法publicstaticvoidmain(String[]args ... [详细]
  • 面向对象之3:封装的总结及实现方法
    本文总结了面向对象中封装的概念和好处,以及在Java中如何实现封装。封装是将过程和数据用一个外壳隐藏起来,只能通过提供的接口进行访问。适当的封装可以提高程序的理解性和维护性,增强程序的安全性。在Java中,封装可以通过将属性私有化并使用权限修饰符来实现,同时可以通过方法来访问属性并加入限制条件。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
author-avatar
手机用户2502905647
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有