- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序,在单击“统计”后我调用 StatsFragment。 StatsFragment 使用fragment_stats xml 布局,其中有TabLayout 和CustomViewPager。但是,当我单击“统计”时,我的 StatsFragment 被称为数据,但不会在那里绘制。当我点击一些选项卡后,数据就会出现。问题出在初始绘图上。来自 How can you tell when a layout has been drawn?我知道我会以某种方式使用 ViewTreeObserver,但找不到方法和方式。
fragment_stats 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"
tools:context=".StatsFragment">
<!-- TODO: Update blank fragment layout -->
<!--android:elevation="6dp"-->
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<pv239.fi.muni.cz.moneymanager.CustomViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"
>
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</pv239.fi.muni.cz.moneymanager.CustomViewPager>
</RelativeLayout>
tab_fragment xml 文件:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/gridViewScreenStats">
<com.jjoe64.graphview.GraphView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/graph" />
<GridLayout
android:id="@+id/stats"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/graph"
android:layout_gravity="center"
android:background="@drawable/box"
android:layout_margin="10dp"
android:rowCount="5"
android:columnCount="2"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting balance: "
android:paddingLeft="10dp"
android:id="@+id/startBalanceLabel"
android:layout_row="0"
android:layout_column="0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/startBalanceLabel"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/startBalance"
android:layout_row="0"
android:layout_column="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/startBalance"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Ending balance: "
android:paddingLeft="10dp"
android:id="@+id/endBalanceLabel"
android:layout_row="1"
android:layout_column="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/startBalance"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:layout_toRightOf="@id/endBalanceLabel"
android:id="@+id/endBalance"
android:layout_row="1"
android:layout_column="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Incomes: "
android:layout_below="@id/endBalanceLabel"
android:id="@+id/incomeStats"
android:layout_row="2"
android:layout_column="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:textColor="@color/recordPositiveValue"
android:id="@+id/incomeSumStats"
android:layout_below="@id/endBalance"
android:textAlignment="textEnd"
android:layout_row="2"
android:layout_column="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Expenses:"
android:id="@+id/expenseStats"
android:layout_row="3"
android:layout_column="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:textColor="@color/recordNegativeValue"
android:id="@+id/expenseSumStats"
android:textAlignment="textEnd"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_row="3"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="+/- status:"
android:id="@+id/actualStats"
android:layout_row="4"
android:layout_column="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/actualSumStats"
android:textColor="@color/recordPositiveValue"
android:textAlignment="textEnd"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_row="4"
android:layout_column="1" />
</GridLayout>
<!-- Income records -->
<ListView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:background="@drawable/box"
android:layout_below="@id/stats"
android:layout_margin="10dp"
android:id="@+id/listViewIncomeStats"
android:nestedScrollingEnabled="true"/>
<!-- Expenses records -->
<ListView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:background="@drawable/box"
android:layout_below="@+id/listViewIncomeStats"
android:layout_margin="10dp"
android:id="@+id/listViewExpences"
android:nestedScrollingEnabled="true"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
StatsFragment java 文件:
public class StatsFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private ListView incomeListView;
private ListView expensesListView;
private OnStatsInteractionListener mListener;
public StatsFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment StatsFragment.
*/
// TODO: Rename and change types and number of parameters
public static StatsFragment newInstance(String param1, String param2) {
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
StatsFragment fragment = new StatsFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_stats, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TabLayout tabLayout = (TabLayout) getView().findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("7 days"));
tabLayout.addTab(tabLayout.newTab().setText("1 month"));
tabLayout.addTab(tabLayout.newTab().setText("1 year"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final CustomViewPager viewPager = (CustomViewPager) getView().findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setPagingEnabled(false);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
// TextView txt = (TextView) (adapter.getItem(tab.getPosition())).getView().findViewById(R.id.startMonthStats);
processChanges((adapter.getItem(tab.getPosition())).getView(),tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onStatsInteraction(uri);
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnStatsInteractionListener {
// TODO: Update argument type and name
void onStatsInteraction(Uri uri);
}
private Date numbersToDate(int daysBack, int monthsBack, int yearsBack)
{
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, daysBack *(-1));
cal.add(Calendar.MONTH, monthsBack *(-1));
cal.add(Calendar.YEAR, yearsBack *(-1));
return cal.getTime();
}
public void processChanges(View tabView, int tabNum)
{
int d =7;
int m =0;
int y =0;
MMDatabaseHelper sloh = MMDatabaseHelper.getInstance(getActivity());
if (tabNum == 1)
{
d =0;
m =1;
y =0;
}
else if (tabNum == 2)
{
d =0;
m =0;
y =1;
}
createGraph(tabView, d, m, y, sloh);
setBalances(tabView, d, m, y, sloh);
setListValues(tabView, d, m, y, sloh);
}
private void setListValues(View tabView, int d, int m, int y,MMDatabaseHelper sloh) {
// Creating incomes list
incomeListView = (ListView) tabView.findViewById(R.id.listViewIncomeStats);
Cursor incomeRecords = sloh.getRecordsInRange(">",d,m,y);
RecordsDbToStatsAdapter incomeAdapter = new RecordsDbToStatsAdapter(this.getContext(), incomeRecords, 0);
incomeListView.setAdapter(incomeAdapter);
// Creating expenses list
expensesListView = (ListView) tabView.findViewById(R.id.listViewExpences);
Cursor expensesRecords = sloh.getRecordsInRange("<",d,m,y);
RecordsDbToStatsAdapter expensesAdapter = new RecordsDbToStatsAdapter(this.getContext(), expensesRecords, 0);
expensesListView.setAdapter(expensesAdapter);
}
private void setBalances(View tabView, int d, int m, int y, MMDatabaseHelper sloh) {
//Fetching Values of incomes and expenses and balances
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.getDefault());
format.setMaximumFractionDigits(2);
TextView incSum = (TextView) tabView.findViewById(R.id.incomeSumStats);
Integer helpInc = sloh.getSumRecordsInRange(">",d,m,y);
BigDecimal incValue = new BigDecimal(helpInc.toString());
incSum.setText(format.format(incValue.abs().setScale(2).doubleValue()));
TextView expSum = (TextView) tabView.findViewById(R.id.expenseSumStats);
Integer helpExp = sloh.getSumRecordsInRange("<",d,m,y);
BigDecimal expValue = new BigDecimal(helpExp.toString());
expSum.setText(format.format(expValue.abs().setScale(2).doubleValue()));
TextView startBal = (TextView) tabView.findViewById(R.id.startBalance);
BigDecimal startValue = new BigDecimal(sloh.getStartingBal(numbersToDate(d,m,y)).toString());
startBal.setText(format.format(startValue.abs().setScale(2).doubleValue()));
TextView endBal = (TextView) tabView.findViewById(R.id.endBalance);
BigDecimal endValue = new BigDecimal(sloh.getEndingBal().toString());
endBal.setText(format.format(endValue.abs().setScale(2).doubleValue()));
TextView actState = (TextView) tabView.findViewById(R.id.actualSumStats);
actState.setText(format.format(new BigDecimal(helpInc + helpExp).setScale(2).doubleValue()));
}
private void createGraph(View tabView, int d, int m, int y, MMDatabaseHelper sloh) {
// Graph rendering section
// Cursor graphCursor = sloh.getRecordsInRange(null,d,m,y);
// graphCursor.moveToFirst();
int days=0;
if (d == 7) { days = d;}
if (m == 1) { days = 28;}
if (y == 1) { days = 365;}
ArrayList numList = new ArrayList();
GraphView graph = (GraphView) tabView.findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] { });
Calendar day = Calendar.getInstance();
day.set(Calendar.HOUR_OF_DAY,0);
day.set(Calendar.MINUTE,0);
day.set(Calendar.SECOND,0);
for(int i=days; i>0;i--) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR,-i);
day.setTime(cal.getTime());
Log.i("TIME",cal.getTime().toString());
numList.add(sloh.getCurrentBalanceForOneDay(cal.getTime()));
series.appendData(new DataPoint(day.getTime(),sloh.getCurrentBalanceForOneDay(cal.getTime())),true,days);
}
Log.i("Pole", numList.toString());
graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity()));
graph.getGridLabelRenderer().setNumHorizontalLabels(4); // only 4 because of the space
graph.addSeries(series);
}
}
最佳答案
据我所知,您没有在正确的时间做正确的事情。当调用 fragment 的 onCreateView
时,您会膨胀相应的布局,但不会在其中设置任何值。
只需将 onActivityCreated
中的所有代码交换到 onCreateView
中即可解决问题,如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_stats, container, false);
TabLayout tabLayout = (TabLayout)layout.findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("7 days"));
tabLayout.addTab(tabLayout.newTab().setText("1 month"));
tabLayout.addTab(tabLayout.newTab().setText("1 year"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final CustomViewPager viewPager = (CustomViewPager) layout.findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setPagingEnabled(false);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
processChanges((adapter.getItem(tab.getPosition())).getView(),tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return (layout);
}
编辑:在第二次(更仔细地)查看您的代码后,最好的想法是让 fragment 管理您的选项卡,以便您可以将逻辑放在那里(并将它们放置在每个选项卡中)。为此:
第 1 步:为您的页面创建一个新的 fragment 类
public abstract class PageFragment extends Fragment {
private int pageNumber;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt("page_number", page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return (fragment);
}
public PageFragment(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
pageNumber = getArguments().getInt("page_number");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.tab_fragment, container, false);
//Get all the views you need for your page, using
//layout.findViewById();
//Then you will need to call processChanges for that page
processChanges(layout, pageNumber);
return (layout);
}
//Also add here all the logic associated with processChanges
}
第 2 步是在 StatsFragment 中获取 fragment 的引用,并将这些 fragment 添加到寻呼机中:
private PageFragment pageOne;
private PageFragment pageTwo;
private PageFragment pageThree;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_stats, container, false);
TabLayout tabLayout = (TabLayout)layout.findViewById(R.id.tab_layout);
pageOne = PageFragment.newInstance(0);
pageTwo = PageFragment.newInstance(1);
pageThree = PageFragment.newInstance(2);
tabLayout.addFragment(pageOne, "7 days");
tabLayout.addFragment(pageTwo, "1 month");
tabLayout.addFragment(pageThree, "1 year");
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
}
第 3 步:您最终需要一个自定义寻呼机适配器来保存寻呼机的标题和 fragment :
public class MyViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments = new ArrayList<>();
private List<String> mTitles = new ArrayList<>();
public MyViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return (mFragments.get(position));
}
@Override
public int getCount() {
return (mFragments.size());
}
@Override
public String getPageTitle(int position) {
return (mTitles.get(position));
}
}
要使用它,只需通过(而不是您的 PagerAdapter
)创建它
final MyPagerAdapter adapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
关于java - 如何在布局内部绘制布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37696647/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!