use*_*667 3 android android-fragments android-view
简而言之,问题是我可以通过id找到我的元素,但它一直要求我将它转换为视图,我不能将它转换为我的对象名称类型.我不确定转换为查看是否是假设,但Android示例只是创建片段并添加它因此它没有在xml中定义.但关键是在示例中,对象不是类型视图.
更多信息:这可能是一个快速简便的解决方案.我有一个片段类,它包含我用代码创建的自定义视图(它是一种不断绘制的游戏).我在xml布局的左上角添加了片段.片段下面是按钮(在xml布局中定义).我有一个主要的活动,我想要一个包含我自定义视图的片段实例的句柄,这样当单击按钮时,片段中的一些变量会被更改,并且视图会被重绘.
XML:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment android:name="com.example.dcubebluetooth"
android:id="@+id/lEDView1"
android:layout_width="400px"
android:layout_height="400px" />
<Button
android:id="@+id/layer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lEDView1"
android:layout_marginTop="50px"
android:text="Layer1"
android:textSize="12dp" />
<Button
android:id="@+id/layer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer1"
android:layout_toRightOf="@id/layer1"
android:text="Layer2"
android:textSize="12dp" />
<Button
android:id="@+id/layer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer2"
android:layout_toRightOf="@id/layer2"
android:text="Layer3"
android:textSize="12dp" />
<Button
android:id="@+id/layer4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer3"
android:layout_toRightOf="@id/layer3"
android:text="Layer4"
android:textSize="12dp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
主要活动:
public class MainActivity extends Activity implements LEDGridFragment.TriggerBlueTooth{
Button l1;
Button l2;
Button l3;
Button l4;
BluetoothService bt;
LEDGridFragment gridUI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
@Override
public void onledSelected(int layer, int led) {
// TODO Auto-generated method stub
String msg = Integer.toString(layer);
if(led<10) msg+=0;
msg+= Integer.toString(led);
bt.write(msg);
}
//Full method below not showing. I don't know why
public void initialize(){
gridUI = findViewById(R.id.lEDView1);
bt = new BluetoothService();
l1 = (Button) findViewById(R.id.layer1);
l2 = (Button) findViewById(R.id.layer2);
l3 = (Button) findViewById(R.id.layer3);
l4 = (Button) findViewById(R.id.layer4);
l1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 0;
gridUI.update();
}
});
l2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 1;
gridUI.update();
}
});
l3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 2;
gridUI.update();
}
});
l4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 3;
gridUI.update();
}
});
Run Code Online (Sandbox Code Playgroud)
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
分段:
public class LEDGridFragment extends Fragment{
LEDView view;
TriggerBlueTooth mCallBack;
boolean[][][] states = new boolean[4][4][4];
int currentLayer = 0;
public interface TriggerBlueTooth{
public void onledSelected(int layer, int led);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallBack = (TriggerBlueTooth) activity;
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) (event.getX())/100;
int y = (int) (event.getY())/100;
int led = y+(x*4);
mCallBack.onledSelected(currentLayer, led); //Notify Main Activity of the led that was toggled
//So it can send data over Bluetooth
return false;
}
});
}
public void update(){
view.drawGrid(states, currentLayer); //Update the view
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
for(int i=0; i<states.length; i++){ //Initialize the board off which the view will be drawn
for(int j=0; j<states[0].length; j++){
for(int k=0; k<states[0][0].length; k++){
states[i][j][k] = false;
}
}
}
view = new LEDView(getActivity());
return view;
//return super.onCreateView(inflater, container, savedInstanceState); //Auto-formed. Commented out
}
}
Run Code Online (Sandbox Code Playgroud)
在旁边(好吧,如果你不回答这个):图形布局没有显示它在游戏的初始绘图方面的假设.如果我添加视图本身,它确实如此.但是现在,它是一个带有"从'片段布局'上下文菜单中选择预览布局的灰色框"这个菜单在哪里?我想知道我的观点是否有效.我不能亲自测试它,因为1.它没有编译,2.我没有Android手机
kco*_*ock 17
这应该这样做:
FragmentManager mgr = getFragmentManager();
Fragment fragment = mgr.findFragmentById(R.id.lEDView1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2479 次 |
| 最近记录: |